2

I cant seem to figure out why the creation of this custom object is not getting hit by my test class. Any help would be greatly appreciated.

enter image description here

Here is the test class:

@isTest
public class TestCreateReviewsController {
    @isTest
    private static void init(){

        Account testAccount = new Account(name='Test Company Name');
        insert testAccount;
        Case ca = new Case(Subject='Test Controller Acct Case');
        ca.AccountId = testAccount.Id;
        insert ca;

        List<Case> cases = new List<Case>();
        cases.add([ Select Id From Case Where Id = : ca.Id ]);

        // start the test execution context
        Test.startTest();

        // load the page       
        PageReference pageRef = Page.MassCreateCaseReview;
        pageRef.getParameters().put('Id',ca.Id);
        Test.setCurrentPageReference(pageRef);

        //load the controller extension
        CreateReviewsController crc = new CreateReviewsController(new ApexPages.StandardSetController( cases ));

        crc.createReviews();
        List<Case_Review__c> crIds = new List<Case_Review__c>();
        for(Case cs : cases){
            Case_Review__c crS = new Case_Review__c();
            crS.Case__c = cs.Id;
            crIds.add(crS);
        }
        insert crIds;
        crc.getMyReviews();

        Test.stopTest();




    }
}
5

You never set the selected records. Break out your controller and extension constructors so you can do so.

ApexPages.StandardSetController controller = new ApexPages.StandardSetController(cases);
controller.setSelected(cases);
CreateReviewsController extension = new CreateReviewsController(controller);

Also, you can simplify a lot of your setup related to cases by just constructing and inserting the list. No need to query or call List.add anywhere.

List<Case> cases = new List<Case> { new Case(Subject='...', AccountId=account.Id) };
insert cases;

One more thing: it's always worth reading Difference between controller and extensions!

  • 3
    Beat me to it :) – Eric Dec 28 '16 at 15:24
  • Awesome! I knew it was setting the selected cases somewhere. Thank you so much. – Allen Mann Dec 28 '16 at 15:25

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.