0

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?

  • try to move this line packController pack2 = new packController(); outside Test.stopTest() – Tushar Sharma Sep 17 '18 at 5:32
1

If possible, use two unit tests. The first does the method with the callout, the other with everything else. You may need to use @TestVisible on some of the attributes to manipulate the controller's internal state. This makes it easier to provide real coverage, including validating logic, instead of just a plain code-coverage/smoke-screen test. The error regarding not being able to call out after a DML operation is rather easily solved by using multiple tests. Also, you can use @TestSetup to set up your data, reducing common DML operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.