+ Start a Discussion
Steve ChadbournSteve Chadbourn 

How to unit test page references

How do I unit test methods in my controller that return a page reference? In particular how do I test that the page returned is the one I expect?
 
Here is a simple method returning a page reference:
 
Code:
public PageReference policiesPage()
{
 return Page.newClaimWzrdPolicies;
}

 
I can't seem to test like this:
 
Code:
PageReference policiesPage = controller.policiesPage();

system.assert( policiesPage == Page.newClaimWzrdPolicies ); 

 
because I'm testing that two instances of an object are the same and I'm guessing they are not for some reason.
 
So how do I test I'm getting the correct page back? I have other methods that can return one of a number of pages depending on certain conditions so I need to test all conditions and all returned pages.
 
Has anyone done this?
Ron HessRon Hess
you can try this, it's a pretty good simple test


Code:


PageReference policiesPage = controller.policiesPage();

system.assert( policiesPage.getUrl().contains('newClaimWzrdPolicies') );

 



Message Edited by Ron Hess on 05-05-2008 05:34 PM
Steve ChadbournSteve Chadbourn

Thanks Ron.

The assertion failed initially and I was unsure why until I wrote the url to the debug log. The url appears to be all lower case. Is this standard behaviour?

I'll probably force it anyway to be safe:

string policiesPageUrl = policiesPage.getUrl().ToLowerCase();

system.assert( policiesPageUrl.contains('newclaimwzrdpolicies') );
 
 
thinhtvuthinhtvu

In order to get the test to run correctly, you can lowercase the string within in the 'contains' part. So yes, it's case sensitive.