我是 apex 的新手,我创建了一个按钮来通过视觉力页面调用 apex 类。 这是我的视觉力页面代码。
<apex:page standardController="Opportunity"
extensions="myclass"
action="{!autoRun}">
</apex:page>
这是我的顶点类(class)。
public class myclass {
private final Opportunity o;
String tmp;
public myclass(ApexPages.StandardController stdController) {
this.o = (Opportunity)stdController.getRecord();
}
public PageReference autoRun() {
String theId = ApexPages.currentPage().getParameters().get('id');
for (Opportunity o:[select id, name, AccountId, from Opportunity where id =:theId]) {
//Create the Order
Order odr = new Order(
OpportunityId=o.id
,AccountId = o.AccountId
,Name = o.Name
,EffectiveDate=Date.today()
,Status='Draft'
);
insert odr;
tmp=odr.id;
}
PageReference pageRef = new PageReference('/' + tmp);
pageRef.setRedirect(true);
return pageRef;
}
}
我想创建测试类。 我不知道如何从测试类中引用 PageReference autoRun() 方法。如果有人可以告诉我有关此 apex 类的测试类的信息,那么大家需要帮助。
最佳答案
您需要为插入的机会配置标准 Controller 。然后将StandardController传递给构造函数,然后再调用方法进行测试。
例如
public static testMethod void testAutoRun() {
Opportunity o = new Opportunity();
// TODO: Populate required Opportunity fields here
insert o;
PageReference pref = Page.YourVisualforcePage;
pref.getParameters().put('id', o.id);
Test.setCurrentPage(pref);
ApexPages.StandardController sc = new ApexPages.StandardController(o);
myclass mc = new myclass(sc);
PageReference result = mc.autoRun();
System.assertNotEquals(null, result);
}
关于unit-testing - 如何从 apex 测试类引用 PageReference 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31457529/