Apex, why the standard controller does not set the current page when creating the controller (Unit Test)

The Apex Unit verifies why MyConrtoller myCont = new MyController (StandardContoller); call to set current page?

For example, if I have this page:

<apex:page standardController="DB_Object__c" extensions="MyExtension">
  <apex:form id="detail_list">
    <apex:detail />
    <apex:actionStatus id="readStatus">
    <apex:facet name="start">
        Loading, please wait...
   </apex:facet>
    <apex:facet name="stop"><apex:outputPanel >
        <apex:commandButton action="{!readData}"
            value="Update Data"
            rerender="detail_list"
            status="readStatus"/>
        {!remainingRecords}</apex:outputPanel>
    </apex:facet>
   </apex:actionStatus>
   </apex:form>
</apex:page> 

If my unit tests create this:

DB_Object__c dbObj = new DB_Object__c();
dbObj.Name = 'test';
dbObj.Setting = 'aSetting';
insert dbObj;
Test.setCurrentPageReference(Page.Demo);
ApexPages.StandardController sc = new ApexPages.StandardController(dbObj);
MyExtension myExt = new MyExtension(sc);

Why ApexPages.currentPage().getParameters().get('id');is failing? I have to do:

ApexPages.currentPage().getParameters().put('id',dbObj.id);

What is the point of passage in dbObjto StandardControllerif she does nothing with it? Is the intent you send to an empty object and extenstion uses that object? It seems that there is not a lot of documentation for standard controllers and unit testing ...

Thank!

+5
source share
3 answers

:

// at first you have to create the object
DB_Object__c dbObj = new DB_Object__c();
dbObj.Name = 'test';
dbObj.Setting = 'aSetting';
insert dbObj;

// then you'd call the vf page with the id query paramter
Test.setCurrentPageReference(Page.Demo);
ApexPages.currentPage().getParameters().put('id', dbObj.Id);
dbObj = [Select All, Fields, You, Need From DB_Object__c Where Id = :ApexPages.currentPage().getParamters().get('id')];

// then the StandardController & controller extension get initiated
ApexPages.StandardController sc = new ApexPages.StandardController(dbObj);
MyExtension myExt = new MyExtension(sc);

, getParameters().get('id') ? StandardController , , save PageReference.

+2

, StandardController , visualforce. , , StandardController .

, StandardController id URL-. .

  • Id List id=ApexPages.currentPage().getParameters().get('id');
  • Sobject Id My_Object my_Object=[SELECT Id, Name FROM My_Object WHERE Id = :id]
  • , ApexPages.StandardController sc = new ApexPages.StandardController(my_Object)
  • , MyExtension myExt = new MyExtension(sc).

, StandardController sObject sObject, . , , , ApexPages.currentPage().getParameters().put('id', dbObj.Id);.

0

, , API

String.valueOf(URL.getCurrentRequestUrl()).toLowerCase().contains('services/soap')
-1
source

All Articles