0

I created a list button that launches a vf page. The vf page is a form that creates multiple records based on the form input and what records were selected from the list view. It works as expected. My problem is my test coverage.

These are the only lines that my test is not reaching. It looks like my list of selectedContacts is empty in the test.

enter image description here

Controller

public with sharing class ContactMassSendSurveyController 
{    
    private ApexPages.StandardSetController standardController;

    // constructor.
    public ContactMassSendSurveyController(ApexPages.StandardSetController standardController)
    {
        this.standardController = standardController;

    }

    // get a list of surveys for the form picklist
    public List<SelectOption> getSurveys() {
        List<SelectOption> surveys = new List<SelectOption>();
        for (SEO__CSEO_Survey__c s : [SELECT Id, Name FROM SEO__CSEO_Survey__c])
        {
            surveys.add(new SelectOption(s.Id , s.Name));
        }

        return surveys;
    } 

    public PageReference sendSurveys()
    {       

        List<Contact> selectedContacts = (List<Contact>) standardController.getSelected();

        String selectedSurvey = Apexpages.currentPage().getParameters().get('selectedSurvey');
        DateTime sendDateTime = DateTime.parse(Apexpages.currentPage().getParameters().get('sendDateTime'));


        // an empty list to add records to
        List<SEO__Survey_Sent__c> surveysToSend = new List<SEO__Survey_Sent__c>();

        for(Contact c : selectedContacts) {
            SEO__Survey_Sent__c s        = new SEO__Survey_Sent__c ();
            s.SEO__Contact__c            = c.Id;
            s.SEO__Survey__c             = selectedSurvey;
            s.SEO__Cust_Survey_Email__c  = [SELECT Email FROM Contact WHERE Id =: c.Id].Email;
            s.SEO__Survey_Sent__c        = sendDateTime;
            surveysToSend.add(s);
        }

        insert surveysToSend;
        return standardController.cancel();   
    }
}

When establishing my controller I put a SOQL in it to get the selected record. I believe that is the correct way of doing it.

Test Controller

@isTest
public class ContactMassSendSurveyControllerTest {
    static testMethod void massSendSurvey(){
        //Create Test Data

        Contact c = new Contact(FirstName='John', LastName='Doe', Email='john@doe.email');
        insert c;

        SEO__CSEO_Survey__c survey = new SEO__CSEO_Survey__c(Name = 'Test Survey');

        insert survey;

        // load the page       
        PageReference pageRef = Page.ContactMassSendSurvey;
        pageRef.getParameters().put('selectedSurvey', survey.Id);
        pageRef.getParameters().put('sendDateTime', '01/01/2001 7:32 PM');

        Test.setCurrentPageReference(pageRef);

        // load the extension
        ContactMassSendSurveyController controller = new ContactMassSendSurveyController(new ApexPages.StandardSetController([SELECT Id FROM Contact]));

        controller.getSurveys();

        controller.sendSurveys();

    }
}
2

This pattern may help to get the coverage. there is no setter method for this

public PageReference sendSurveys(){
    List<Contact> selectedContacts = (List<Contact>) standardController.getSelected();
    return manageSelected(selectedContacts);

}

@TestVisible
private pageReference manageSelected(List<Contact> selectedContacts){
        String selectedSurvey = Apexpages.currentPage().getParameters().get('selectedSurvey');
        DateTime sendDateTime = DateTime.parse(Apexpages.currentPage().getParameters().get('sendDateTime'));


        // an empty list to add records to
        List<SEO__Survey_Sent__c> surveysToSend = new List<SEO__Survey_Sent__c>();

        for(Contact c : selectedContacts) {
            SEO__Survey_Sent__c s        = new SEO__Survey_Sent__c ();
            s.SEO__Contact__c            = c.Id;
            s.SEO__Survey__c             = selectedSurvey;
            s.SEO__Cust_Survey_Email__c  = [SELECT Email FROM Contact WHERE Id =: c.Id].Email;
            s.SEO__Survey_Sent__c        = sendDateTime;
            surveysToSend.add(s);
        }

        insert surveysToSend;
        return standardController.cancel(); 
} 
2

You need to use StandardSetcontoller object instance and use setSelected (contacts) method to assign selectedContact values

Refer setSelected(selectedRecords)

  • Like this? ContactMassSendSurveyController controller = new ContactMassSendSurveyController(new ApexPages.StandardSetController.setSelected([SELECT Id FROM Contact])); If so no working – Tyler Zika Jul 21 '17 at 15:59

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.