You need to sign in to do that
Don't have an account?
VF Extension Testing
Edit note: This post was a bit epic, so I've edited it down to something that's quicker to read and, hopefully,easier to respond to. Thanks in advance!!!
===
I'm trying to write tests for a VF extension class and am hitting a brick wall with respect to an assert. Any help would be greatly appreciated.
Scenario:
Custom object Assessment__c has View override set to the SelectAssessment VF page:
<apex:page standardController="Assessment__c" extensions="AssessmentSelectionExtension" action="{!viewAssessment}">
<apex:outputText value="{!Assessment__c.Owner.Name}" rendered="false"/>
<apex:outputText value="{!Assessment__c.Account__c}" rendered="false"/>
<apex:outputText value="{!Assessment__c.Recommendation__c}" rendered="false"/>
<apex:outputText value="{!Assessment__c.RecordTypeId}" rendered="false"/>
<apex:pageMessages />
<apex:outputPanel layout="block" rendered="{!errorExists}">
</apex:outputPanel>
</apex:page>
The extension, AssessmenSelectionExtension, sets the user as the owner of the assessment, as long as they do not own any other assessments of the relevant record type related to an account. Everything seems to work fine in manual testing, but my test class is having issues around the assert at the bottom of this test class.
//Create account and assessments
System.debug('Creating test account');
Account z = new Account(Name = 'Acme');
insert z;
System.debug('Creating test assessments');
//Create list to insert
List<Assessment__c> assessments = new List<Assessment__c> ();
Assessment__c a1 = new Assessment__c();
a1.Account__c = z.Id;
a1.Recommendation__c = 'null';
a1.RecordTypeId = Utility.iefASRT;
a1.OwnerId = Utility.iefOwner;
assessments.add(a1);
assessments.add(a1.clone());
//Insert the assessments
insert assessments;
//Create list of inserted assessments
Assessment__c [] testassess = [SELECT Id, Owner.Name, OwnerId, Account__c, Recommendation__c, RecordTypeId
FROM Assessment__c
WHERE Account__c = :z.Id
AND RecordTypeId = :Utility.iefASRT];
//Create page reference and set to test page
PageReference pageRef = Page.SelectAssessment;
Test.setCurrentPage(pageRef);
//Set URL parameters
ApexPages.currentPage().getParameters().put('id', testassess[0].id);
//Create instance of page controller
ApexPages.StandardController con = new ApexPages.StandardController(testassess[0]);
//Extend controller with extension, referencing base controller
AssessmentSelectionExtension ext = new AssessmentSelectionExtension(con);
//Call method in extension
ext.viewAssessment();
//Assert the url for the page to conduct an assessment
System.assertEquals('/'+testassess[0].Id+'?nooverride=1', ext.viewAssessment().getUrl());
//Populate recommendation on testassess[0]
//testassess[0].Recommendation__c = 'Approve';
//update testassess[0];
Assessment__c [] testassess2 = [SELECT Id, Owner.Name, OwnerId, Account__c, Recommendation__c, RecordTypeId
FROM Assessment__c
WHERE Id = :testassess[0].Id];
//Assert that running user has taken ownership of the assessment
System.assertEquals(UserInfo.getUserId(), testassess2[0].OwnerId);
When I run the test, the owner of the assessment doesn't change to the running user, as it should do, but stays with the queue. The other asserts work and I've manually tested this functionality and the extension works fine, so something must be amiss with the test.
It seems like I'm not calling the extension correctly, so it doesn't execute the change of ownership, although the other posts on extension testing seem to suggest that my code should work.
Please help!
Do you have debug statements in your extension controller that confirm you are executing the code as you intend to?
If so, could it be related to the fact that visualforce controllers run as the system user when invoked from the browser, while your unit tests are running as your regular user? Maybe you haven't got permission to change ownership on the record owned by Utility.iefOwner.
All Answers
Thanks for the tip - I tried it and no joy I'm afraid. It seems to be that the Test.setCurrentPage method isn't working as I expect it to.
Can't fathom how to do it otherwise....
Do you have debug statements in your extension controller that confirm you are executing the code as you intend to?
If so, could it be related to the fact that visualforce controllers run as the system user when invoked from the browser, while your unit tests are running as your regular user? Maybe you haven't got permission to change ownership on the record owned by Utility.iefOwner.
Hi Bob,
Thanks for the useful tips. I did as you suggested and, voila, it revealed the problem: I was inserting the assessments with Recommendation__c as the string 'null' instead of actual null. I'd read through the test code manually (and in an extremely frustrated mood) about twenty times and had missed this. A salutary lesson in good coding practice - system debug statements from hereon in...
Thanks again,
Paddington