+ Start a Discussion
Ronan O'Neill 6Ronan O'Neill 6 

How do I cover my SelectList<Options> in test class

I have a SelectList which is dynamically built from the controller. But it doesn't seem to get run in the Test Class. How can I make sure its covered?
 
public List<SelectOption> getCountyList() {
        List<SelectOption> options = new List<SelectOption>();
        
        String counties = contactInfo[0].Preferred_Counties_for_Volunteering__c;
        
        if(!counties.contains(contactInfo[0].Home_County__c)) {
            
            options.add(new SelectOption(contactInfo[0].Home_County__c, contactInfo[0].Home_County__c));
        }
        
        List<String> prefCounties = counties.split(';', -2);
  		
        for(String pref : prefCounties) {
            
            options.add(new SelectOption(pref,pref));
    
        }
        
        return options;
    }

My Test Class currently
 
@isTest
public class ToTestFindNearby {

    static testMethod void testFindNearby () {
        
        PageReference pageRef = Page.FindNearby; 
        
        Contact contact = new Contact (LastName = 'Test', Are_you_an_Engineer__c = 'Engineer', Home_County__c = 'Waterford' );
        Account school = new Account (Name = 'Test', County__c = 'Waterford', Lat__c=5.0000, Lng__c=10.000, isMapped__c = true);
 		//Test.startTest();
        insert contact;
        insert school; 
        
        system.assertEquals(true, school.isMapped__c);
        
        date myDate = date.newInstance(2015, 11, 21);

        
        School_Visit__c schoolVisit = new School_Visit__c (School__c = school.id, Status__c = 'Open for Volunteering', 
                                      Topic__c = 'Science', How_many_students_in_the_class__c = 15, Requires_Irish_speaker__c = 'No',
                                      Preferred_Time__c = 'Afternoon', Date__c= myDate);
        
        School_Visit__c schoolVisit2 = new School_Visit__c (School__c = school.id, Status__c = 'Open for Volunteering', 
                                      Topic__c = 'Engineering', How_many_students_in_the_class__c = 15, Requires_Irish_speaker__c = 'No',
                                      Preferred_Time__c = 'Afternoon');
        
        School_Visit__c schoolVisit3 = new School_Visit__c (School__c = school.id, Status__c = 'Open for Volunteering', 
                                      Topic__c = '', How_many_students_in_the_class__c = 15, Requires_Irish_speaker__c = 'No',
                                      Preferred_Time__c = 'Afternoon');
        insert schoolVisit;
        insert schoolVisit2;
        insert schoolVisit3;
        //Test.stopTest();
       
        pageRef.getParameters().put('id', contact.id);
        Test.setCurrentPage(pageRef);
		FindNearby controller = new FindNearby();
        controller.backToContact();
        
    }
}

 
Best Answer chosen by Ronan O'Neill 6
surasura
just direclty call the method via you controller instance cre4ated in the test class 

eg:
FindNearby controller = new FindNearby();
controller.getCountryList();

 

All Answers

surasura
just direclty call the method via you controller instance cre4ated in the test class 

eg:
FindNearby controller = new FindNearby();
controller.getCountryList();

 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
You need to call like below
controller.backToContact();
		 List<SelectOption> lst = controller.getCountyList();


Please try below code:-
@isTest
public class ToTestFindNearby {

    static testMethod void testFindNearby () {
        
        PageReference pageRef = Page.FindNearby; 
        
        Contact contact = new Contact (LastName = 'Test', Are_you_an_Engineer__c = 'Engineer', Home_County__c = 'Waterford' );
        Account school = new Account (Name = 'Test', County__c = 'Waterford', Lat__c=5.0000, Lng__c=10.000, isMapped__c = true);
 		//Test.startTest();
        insert contact;
        insert school; 
        
        system.assertEquals(true, school.isMapped__c);
        
        date myDate = date.newInstance(2015, 11, 21);

        
        School_Visit__c schoolVisit = new School_Visit__c (School__c = school.id, Status__c = 'Open for Volunteering', 
                                      Topic__c = 'Science', How_many_students_in_the_class__c = 15, Requires_Irish_speaker__c = 'No',
                                      Preferred_Time__c = 'Afternoon', Date__c= myDate);
        
        School_Visit__c schoolVisit2 = new School_Visit__c (School__c = school.id, Status__c = 'Open for Volunteering', 
                                      Topic__c = 'Engineering', How_many_students_in_the_class__c = 15, Requires_Irish_speaker__c = 'No',
                                      Preferred_Time__c = 'Afternoon');
        
        School_Visit__c schoolVisit3 = new School_Visit__c (School__c = school.id, Status__c = 'Open for Volunteering', 
                                      Topic__c = '', How_many_students_in_the_class__c = 15, Requires_Irish_speaker__c = 'No',
                                      Preferred_Time__c = 'Afternoon');
        insert schoolVisit;
        insert schoolVisit2;
        insert schoolVisit3;
        //Test.stopTest();
       
        pageRef.getParameters().put('id', contact.id);
        Test.setCurrentPage(pageRef);
		FindNearby controller = new FindNearby();
        controller.backToContact();
		 List<SelectOption> lst = controller.getCountyList();	
    }
}

Please mark this as best solution if this will help you.

Thanks
Amit Chaudhary
Ronan O'Neill 6Ronan O'Neill 6
Sura's seems to work perfectly. Thank you. 

I kind of forgot it was a method cause it was all the way up in Variable country.