function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MargiroMargiro 

Quick PageRef Test Question

Im working on a part of a class that creates a wizard but I cant successfully write a test code for it. I have one below and I cant understand why it doesnt work. Why wont this code work?

 

Heres the part of the class that needs testing-

 

public PageReference step1() { return Page.mhform1; } public PageReference step2() { return Page.mhform2; } public PageReference step3() { return Page.mhform3; } public PageReference step4() { return Page.mhformconfirm; }

 

 Heres what I have made for a test code so far-

@isTest private class testGLeadExtention { static testMethod void step1Test() { // TO DO: implement unit test PageReference step1 = Page.mhform1; Test.setCurrentPageReference(step1); } static testMethod void step2Test() { // TO DO: implement unit test PageReference step2 = Page.mhform2; Test.setCurrentPageReference(step2); } static testMethod void step3Test() { // TO DO: implement unit test PageReference step3 = Page.mhform3; Test.setCurrentPageReference(step3); } static testMethod void step4Test() { // TO DO: implement unit test PageReference step4 = Page.mhformconfirm; Test.setCurrentPageReference(step4); } }

 


 

jkucerajkucera

A bit out of my element, but I believe you need to instantiate an instance of the controller.  Here's how I got mine to pass for a campaign member controller extension which gets variables from the URL:

 

//In this case, the Visualforce page named 'success' is the starting point of this test method. 
PageReference pageRef = Page.Web2CampaignMember;

Test.setCurrentPage(pageRef);

CampaignMember cm=new CampaignMember();
// insert cm;

ApexPages.StandardController sc = new ApexPages.StandardController(cm);
UpdateCampaignMemberClass scExt = new UpdateCampaignMemberClass(sc);

//add parameters to the page
ApexPages.CurrentPage().getParameters().put('id', cm.id);
ApexPages.CurrentPage().getParameters().put('key', cm.EncryptedKey__c);

//Instantiate a new controller with all parameters in the page
scExt = new UpdateCampaignMemberClass(sc);

PageReference retURL= scExt.checkEncryptionKey();
Test.setCurrentPage(retURL);

 There's a great guide on visualforce tests here.

 

Scroll down to find the section on apex controllers.