I have a test class below that is successful and covers accessing my controller with a callout:
@isTest static void testUpdateOfPack(){
MockPackageService mockRes = new MockPackageService();
Test.setMock(HttpCalloutMock.class, mockRes);
PageReference page = new PageReference('/package');
page.getParameters().put('actId', GTRdata.grabAccount().id);
Test.setCurrentPage(page);
packController pack = new packController();
Test.startTest();
//Pack data is entered here
//item pack is entered here
pack.recalculate);
pack.Reduction();
//More pack updates here
pack.save();
Test.stopTest();
itemPack = [SELECT ID, Price__c FROM item_package__c WHERE ID = :itempack.id];
System.assertEquals(924.00, itempack.Price__c);
}
I need to also add a second "PackController pack2 = new PackController(); because when a user goes to edit the record, I need to write a test class to cover that things are being updated
@isTest static void testUpdateofPack(){
MockPackageService mockRes = new MockPackageService();
Test.setMock(HttpCalloutMock.class, mockRes);
PageReference page = new PageReference('/package');
page.getParameters().put('actId', GTRdata.grabAccount().id);
Test.setCurrentPage(page);
packController pack = new packController();
Test.startTest();
//Pack data is entered here
//itemPack is inserted as well
pack.recalculate);
pack.Reduction();
//More pack updates here
pack.save();
itemPack = [SELECT ID, Price__c FROM item_package__c WHERE ID = :itempack.id];
System.assertEquals(924.00, pack.Price__c);
PageReference pageTwo = new PageReference('/pack');
page2.getParameters().put('actId', GTRdata.grabAccount().id);
Test.setCurrentPage(page2);
packController pack2 = new packController();
//More pack updates here
pack2.save();
itemPack = [SELECT ID, Price__c FROM item_package__c WHERE ID = :itempack.id];
System.assertEquals(950.00, itempack.Price__c);
However now, I get an error at packController pack2 = new packController();:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Do I have to implement test.setMock again? Or how would I tackle this?
packController pack2 = new packController();
outsideTest.stopTest()