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
GoForceGoGoForceGo 

Why are controller extension fields not loaded for test method?

	//preq already exists with pending approval status
	Custom_Obj__c pr = [select Status__c from Custom_Obj__c where id = :preq.id];
        System.ASSERT(pr.Status__c == 'Pending Approval'); 
        Test.setCurrentPage(new PageReference('/apex/customObjpage'));
        ApexPages.currentPage().getParameters().put('id',preq.id); 
        stdController = new ApexPages.StandardController(preq);
        customObjController controller = new customObjController(stdController);
        System.ASSERT(controller.customObj.Status__c == 'Pending Approval'); //fails

The status__c field is included in the customObjPage. The status is Pending Approval, but the assert at the end fails. All fields referenced in VF page are supposed to be loaded?

 

 

 

sfdcfoxsfdcfox
1) Use Page.customObjpage instead of partial URL reference.

2) No, the object doesn't magically populate when you place it in a standard controller. You're responsible for either querying the object or populating its fields.
GoForceGoGoForceGo

Thanks.


doesn't getRecord populate fields for extension controllers that are referenced in the VF page? When an end user gets this page, I don't have to query the fields. Why in test methods?

 

The documentation for getRecord says: "Note that only the fields that are referenced in the associated Visualforce markup are available for querying getRecord SObject
on this SObject. All other fields, including fields from any related objects, must be queried using a SOQLexpression."

sfdcfoxsfdcfox
Visualforce automatically generates a query that provides all the fields that are bound. When using a test method, the system doesn't generate this automatic SOQL statement.

At one point, tests used to automatically fail if they had addFields in the constructor and it was called. I still have code in our corporate product that checks if the constructor is called from a test method so that it skips trying to add the fields.
GoForceGoGoForceGo

Thanks.

 

This indeed makes it hard to test extension Controllers -  I have 50+ fields on my VF page and I just have to reload them all after I do anything.

 

I would have thought that Test.setCurrentPage(new PageReference('/apex/customObjpage'))  and other code in testmethod would accomplish the same thing.

 

 

GoForceGoGoForceGo

On a separate note, how do I test a Sites VFPage so that I am running as sites user? Just create a sites user and RunAs? I would want to make sure that when I run as this user  Site.getCurrentSiteUrl() returns a non-null.

 

 

 

sfdcfoxsfdcfox
I'm not doing anything with sites, but I would hazard a guess as to say that the approach sounds reasonable. I don't see any good documentation out there on the web regarding this, but if I get a moment, I'll dig a bit deeper.
GoForceGoGoForceGo

Thanks. I was surprised to see there is no doc. on testing sites as well on web. I will try it as well and post my findings.

 

FYI, to test my extension controller, I just ended up writing a generic query that loads all the fields on my custom object (by using the Sobject's field.getMap() and just iterating over all fields) . Too bad SFDC doesn't have select * from...

 

 

sfdcfoxsfdcfox
I did something similar, except I made a memory cache to avoid re-querying records by ID. Sometimes developers really do need a *, because they just won't know what the user will give them ahead of time.