बनाते समय मानक नियंत्रक वर्तमान पृष्ठ सेट क्यों नहीं करता है शीर्ष यूनिट परीक्षणों में MyConrtoller myCont = new MyController (StandardContoller) क्यों नहीं है; कॉल वर्तमान पृष्ठ सेट?शीर्ष मानक नियंत्रक (यूनिट टेस्ट)
उदाहरण के लिएअगर मैं इस पृष्ठ है:
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);
ApexPages.currentPage().getParameters().get('id');
क्यों असफल है: मेरे इकाई परीक्षण इस बनाता है
<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>
?
ApexPages.currentPage().getParameters().put('id',dbObj.id);
StandardController
को dbObj
में गुजर यह इसके साथ कुछ नहीं करता है, तो की बात क्या है: मैं क्या करना है? क्या आप एक खाली वस्तु में विस्तार करते हैं और extenstion इस वस्तु का उपयोग करता है? मानक नियंत्रक और यूनिट परीक्षण के लिए बहुत सारे प्रलेखन प्रतीत नहीं होते हैं ...
धन्यवाद!
// 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')
विफल करता है:
हालांकि यह काम करता है, यह वास्तव में इसे आदर्श नहीं बनाता है। मुझे लगता है कि अगर मैं एक गैर-शून्य वस्तु में गुजरता हूं तो यह उस ऑब्जेक्ट का उपयोग पृष्ठ के साथ एक नई वस्तु बनाने के बजाय पृष्ठ प्रस्तुत करने के लिए करेगा ... – rjbez