Question
Is there any known reason a SOQL Query would return a runtime System.QueryException
on a custom field in test methods, in particular one one via a parent lookup in a test? I would have thought this is impossible if the apex code compiled. Any one else seeing this sort of behavior?
Background
The simplest case I can get to fail does the following
- Creates a test user with specific profile and does a runAs call
- Create a Parent__c record with field Fail__c
- Create a Child__c record with lookup Parent__c pointing to record from step 2
- Query that record
- Query Exception?
Code would look like this
static testmethod void feelsLikeImTakingCrazyPills() {
system.runAs(generateTestUser()) {
Parent__c parent = new Parent__c(Fail__c = 'wtf');
insert parent;
insert new Child__c(Parent__c = parent.Id);
List<Child__c> children = [SELECT Parent__r.Fail__c FROM Child__c];
// System.QueryException: No Such Column 'Fail__c' on entity 'Parent__c'
}
}
Even more bizarre if I don't populate that field, no issue.
static testmethod void feelsLikeImTakingCrazyPills() {
system.runAs(generateTestUser()) {
Parent__c parent = new Parent__c(Fail__c = 'wtf');
insert parent;
insert new Child__c(Parent__c = null); // <-- i don't populate the lookup here
List<Child__c> children = [SELECT Parent__r.Fail__c FROM Child__c];
// no exception
}
}
And in the actual code that lead to this, the query is in another class. If I make it without sharing no issue.
WTF
Is anyone else seeing this? Seems so confusing:
- Why would I be able to set a field (in other words it's field level security is read and edit) but not query it?
- Why would it matter whether I populated the field?
- Why would
without sharing
make a difference? - Why would a non-dynamic query but not run?
- Am I alone in experiencing this?
Updates
I'm seeing more strange behavior now in the same vein. Trying to create another object, which the test user's profile has perms for, but when I try and insert it I get DML operation INSERT not allowed
, works fine for admin.