Reputation: 393
I am attempting to test this code. I need to set up the controller and set the parameters. CONTROLLER.
The visualForce page has a standard controller of contact
Public Class myClass{
public ApexPages.standardController controller {get; set;}
public string pid {get; set;}
//CONSTRUCTOR
public myClass(ApexPages.StandardController controller){
this.controller = controller;
pId = ApexPages.CurrentPage().getparameters().get('id');
}
TEST CLASS
@isTest
public class testMyClass{
static testMethod void myTest(){
PageReference pageRef = Page.myPage;
Test.setCurrentPageReference(pageRef);
//create contact
Contact cont = new Contact(name ='bob');
insert cont;
ApexPages.CurrentPage().getparameters().put('pid', cont[0].id);
ApexPages.StandardController sc = new ApexPages.standardController(cont[0]);
myClass sic = new myClass(sc);
ApexPages.currentPage().getParameters().put(?);
System.assertEquals('something here');
}
}
Upvotes: 1
Views: 14172
Reputation: 393
Thanks you pointed me in the right direction. Ultimately, this is what worked for me,
Test.setCurrentPage(pageRef);
ApexPages.CurrentPage().getparameters().put('id', cont[0].id)
The way you suggested gives this error " System.QueryException: List has no rows for assignment to SObject"
Upvotes: 1
Reputation: 1189
ApexPages.CurrentPage().getparameters().put('id', cont[0].id);
ApexPages.StandardController sc = new ApexPages.standardController(cont[0]);
myClass sic = new myClass(sc);
System.assertEquals(sic.pid,cont[0].id);
Upvotes: 2