Using test.setMock between test.startTest and test.stopTest
DML performs fine everywhere except:
This code fails unless the insert is not done:
sr = [Select Body From StaticResource Where Name = 'SR Name' LIMIT 1];
String JSONString = sr.body.toString();
Rule__c[] rules = new Rule__c[]{};
for(Rule__c r : (Rule__c[])JSON.deserialize(JSONString, Rule__c[].class)){
rules.add(New Rule__c(
Name = r.Name,
Name__c = r.Name__c,
Handled__c = r.Handled__c,
Text__c = r.Text__c,
JSON_String__c = rJSON_String__c
));
}
//insert rules;
Now, if I take the JSON string that is the body and use it alone without Querying the static resource it works fine
sr = [Select Body From StaticResource Where Name = 'SR Name' LIMIT 1];
String JSONString = test_utils.getJSONString();
Rule__c[] rules = new Rule__c[]{};
for(Rule__c r : (Rule__c[])JSON.deserialize(JSONString, Rule__c[].class)){
rules.add(New Rule__c(
Name = r.Name,
Name__c = r.Name__c,
Handled__c = r.Handled__c,
Text__c = r.Text__c,
JSON_String__c = rJSON_String__c
));
}
insert rules;
The only difference between the two is:
- The body.toString()
- Both methods do indeed have records and are not null
If I do not perform that method then I do not get the uncommitted work pending.
Anyone seen this before or have any other ideas on how to get around it....
vr_rules
is a zero length array meaning no insert is done. Can you confirm thatvr_rules
is not zero length in both cases? (Your pasted code adds to therules
variable but inserts a different variablevr_rules
; you should clean that up too to make sure the code is doing what you think it is.)