unit-testing - 如何从 apex 测试类引用 PageReference 方法

标签 unit-testing testing salesforce visualforce force.com

我是 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/

相关文章:

unit-testing - 未找到转换选项中的模块 <rootDir>/node_modules/vue-jest

java - Mockito 问题 - InvalidUseOfMatchersException

ruby - 有什么工具可以帮助构建 REST Controller 规范?

salesforce - 不接受WITH 子句 - Salesforce

javascript - 如何提供模拟文件来更改 &lt;input type ='file' > 的事件以进行单元测试

JavaScript 单元测试。 mock 什么?

swift - 单元测试 Swift,info.plist 的 CoreLocation 问题

C++:如何为模板类创建小型速度测试?

oauth-2.0 - Force.com OAuth 2.0 JWT 到 Google 服务帐户融合表 API 400 错误请求 Invalid_Grant

Azure数据工厂上传到Salesforce并引用另一个对象上的字段