在Apex單元測試爲什麼MyConrtoller myCont = new MyController(StandardContoller);呼叫設置當前頁面?Apex爲什麼標準控制器在創建控制器時未設置當前頁面(單元測試)
舉例來說,如果我有這個頁面:
<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>
如果我的單元測試創建此:
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');
失敗?我要做的:
ApexPages.currentPage().getParameters().put('id',dbObj.id);
什麼是傳入dbObj
到StandardController
如果它沒有用它做什麼的呢?您是否意圖在空白對象中發送並且擴展使用此對象?似乎沒有很多文件StandardController和單元測試...
謝謝!去這個問題在測試場景中
儘管這確實起作用,但它並不能真正使它變得理想。我會認爲,如果我傳遞一個非空對象,它會使用該對象來呈現頁面,而不是用它創建一個新對象... – rjbez 2012-08-21 20:10:23