When running unit tests, I'm currently getting:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
The issue is that I have a trigger that runs a callout after account update. So I need to set the mock callout before anything would update the account. The current test works if I comment out the second custom object logic (i.e. the second line would cause the trigger to fire again). However, if I send update accounts two separate times, it throws the above error.
My current approach is the following:
static testMethod void testNow() {
Test.startTest();
Test.setMock(HttpCalloutMock.class, new ThisIsMyMockCallout());
List<Account> testAccs = makeNewAccounts(1,10); // Makes list of 1 account
insert testAccs; // This works without mock since it doesn't update acc
List<CustomObject> customObjs = makeNewCustomObs(testAccs);
insert customObjs; // This needs mock as it updates account due to relation
List<CustomObjectA> customObjsA = newMakeCustomObsA(customObjs,testAccs);
insert customObjsA; // This needs mock as it updates account due to relation
Test.stopTest();
// A bunch of assertEquals tests
}
Any help would be great!