0

I write a test class to cover standard set controller class on account but i am not able to pass id. thats why not able cover code coverage.

here is my code

@IsTest
public class ClientReachEmailPageControllerTest {

    private static testMethod void test1() {
        User u = CreateOrder.createUser('[email protected]', 'User Order Maker Details');
        insert u;

        // Create Client
        List<Account> acc= new List<Account>();
        Account client = new Account(LastName='TestAccount', personemail = '[email protected]',Asst_Email__pc = '[email protected]');
        acc.add(client);
        Account client1 = new Account(LastName='TestAccount1', personemail = '[email protected]',Asst_Email__pc = '[email protected]');
        acc.add(client1);

        //insert client;
        insert acc;

        // Create Order
        Order__c hOrder = CreateOrder.createHOrderItem(u.Id, u.Id, acc[0].Id, 'Test  Details');
        hOrder.Coat_Maker1__c ='a066YT000005c2vT';
        insert hOrder;        

        // Create Controller
        Test.startTest();       
        Test.setCurrentPage(Page.ClientReachEmailPage);
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(acc);
        ClientReachEmailPageController obj = new ClientReachEmailPageController(stdSetController);  

        PageReference pageRef = Page.ClientReachEmailPage;        
        Test.setCurrentPage(pageRef);
        Test.stopTest();
    }

}

1 Answer 1

0

For StandardSetController, you don't have to pass the ID, instead you will have to pass the list of records. You can set the records for StandardSetController using setSelected method. Below test class should cover your controller.

@isTest
public class ClientReachEmailPageControllerTest {
    private static testMethod void test1() {
        User u = CreateOrder.createUser('[email protected]', 'User Order Maker Details');
        insert u;

        // Create Client
        List<Account> acc= new List<Account>();
        Account client = new Account(LastName='TestAccount', personemail = '[email protected]',Asst_Email__pc = '[email protected]');
        acc.add(client);
        Account client1 = new Account(LastName='TestAccount1', personemail = '[email protected]',Asst_Email__pc = '[email protected]');
        acc.add(client1);

        //insert client;
        insert acc;

        // Create Order
        Order__c hOrder = CreateOrder.createHOrderItem(u.Id, u.Id, acc[0].Id, 'Test  Details');
        hOrder.Coat_Maker1__c ='a066YT000005c2vT'; //If this is a relationship field, create a record for Coat Maker and assign the Id of that record to this field, if its a text field, it will work else the test class will fail.
        insert hOrder;        

        // Create Controller
        Test.startTest();       
        Test.setCurrentPage(Page.ClientReachEmailPage);
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(acc);
        stdSetController.setSelected(acc);
        ClientReachEmailPageController obj = new ClientReachEmailPageController(stdSetController);  
        Test.stopTest();
    }
}

Note : You should always add assert statements in your test class to check if your code is working as expected.

2
  • That hard-coded Salesforce Id looks like a problem waiting to happen. Is Coat_Maker1__c a Text field? I don't see how the unit test passes otherwise.
    – David Reed
    Jan 31, 2019 at 12:44
  • Yup, you are right @DavidReed, that test class would fail unless it is a text field. Added a comment. Jan 31, 2019 at 12:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .