+ Start a Discussion
ForceComForceCom 

Custom Controller Test Class

Hi 

 

I am facing an issue in writing an test method for the custom controller. 

 

I am not able to capture the page parameters, in the test class.Please find below the code.

 

/*

public class multirecords{
    public List<Registration__c> regs {get; set;}
    public Semester_Schedule__c academic {get; set;}
    Id id = ApexPages.currentPage().getParameters().get('id');
    
    public multirecords(){ 
        
        academic=[Select Id, Name from Semester_Schedule__c where Id=:Id];
            
        regs = new List<Registration__c>();
        regs.add(new Registration__c(Semester_Schedule__c=academic.Id));
        
   
    }
    public void addrow(){
        
        regs.add(new Registration__c(Semester_Schedule__c=academic.Id));
    }
    
    public PageReference save(){
        insert regs;
        PageReference home = new PageReference('/home/home.jsp');
        home.setRedirect(true);
        return home;
    }
}

*/

 

any kind of help, guidance, is truly appreciated.

 

Thanks All

 

TehNrdTehNrd

In your test method you need to set the page parameters like this:

 

 

insert testSemester;

Test.setCurrentPageReference(new PageReference('Page.myPageName')); 
ApexPages.currentPage().getParameters().put('id', opp.id);

 

Also, pretty sure you can't give a variable the name of "id":

 

 

Id semesterId = ApexPages.currentPage().getParameters().get('id');

public multirecords(){ 
        
        academic=[Select Id, Name from Semester_Schedule__c where Id=:semesterId ];