[SalesForce] Unable To Cover StandardSetController.getSelected Loop

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:

public class TestCreateReviewsController {
    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

        // load the page       
        PageReference pageRef = Page.MassCreateCaseReview;

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

        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;
        insert crIds;



Best Answer

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);
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!

Related Topic