Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何从apex测试类引用PageReference方法_Unit Testing_Testing_Salesforce_Visualforce_Force.com - Fatal编程技术网

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

Unit testing 如何从apex测试类引用PageReference方法,unit-testing,testing,salesforce,visualforce,force.com,Unit Testing,Testing,Salesforce,Visualforce,Force.com,我是apex的新手,我创建了一个按钮,通过VisualForce页面调用apex类。 这是我的视觉力页面代码 <apex:page standardController="Opportunity" extensions="myclass" action="{!autoRun}"> </apex:page> 我想创建一个测试类我不知道如何从测试类中引用PageReference autoRun()方法。如果有人能告诉我关于这个apex类的测试类,大家需要帮助 您需要

我是apex的新手,我创建了一个按钮,通过VisualForce页面调用apex类。 这是我的视觉力页面代码

<apex:page standardController="Opportunity" 
extensions="myclass" 
action="{!autoRun}"> 
</apex:page>

我想创建一个测试类我不知道如何从测试类中引用PageReference autoRun()方法。如果有人能告诉我关于这个apex类的测试类,大家需要帮助

您需要为插入的Opportunity配置StandardController。然后在调用方法进行测试之前,将StandardController传递给构造函数

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;
      }
}
例如

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);

}