rjb*_*bez 5 salesforce force.com visualforce apex-code
在Apex Unit测试中,为什么MyConrtoller myCont = new MyController(StandardContoller); 电话设置当前页面?
例如,如果我有这个页面:
<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>
Run Code Online (Sandbox Code Playgroud)
如果我的单元测试创建了这个:
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);
Run Code Online (Sandbox Code Playgroud)
为什么ApexPages.currentPage().getParameters().get('id');失败?我要做:
ApexPages.currentPage().getParameters().put('id',dbObj.id);
Run Code Online (Sandbox Code Playgroud)
如果它没有对它做任何事情,那么传递dbObj给StandardController它的重点是什么?您是否在空白对象中发送的意图和扩展使用此对象?标准控制器和单元测试似乎没有很多文档......
谢谢!
在测试场景中解决这个问题的一种方法是这样的:
// 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);
Run Code Online (Sandbox Code Playgroud)
现在回答你的问题为什么会getParameters().get('id')失败?您可以启动一个没有记录 ID 的 StandardController,如果您使用默认的savePageReference,这将为您提供一条新记录并自动插入该记录。
| 归档时间: |
|
| 查看次数: |
6296 次 |
| 最近记录: |