Как обратиться к методу PageReference из тестового класса вершины

Я новичок в вершине, я создал кнопку для вызова класса вершины через страницу визуальной силы. Вот мой код страницы визуальной силы.

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

Вот мой высший класс.

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

Я хочу создать тестовый класс. Я не знаю, как сослаться на метод autoRun() PageReference из тестового класса. Ребятам нужна помощь, если кто-нибудь может рассказать мне о тестовом классе этого класса вершины.


person waqas ali    schedule 16.07.2015    source источник


Ответы (1)


Вам нужно будет настроить StandardController для вставленной возможности. Затем передайте StandardController для передачи конструктору перед вызовом метода для тестирования.

E.g.

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

}
person Daniel Ballinger    schedule 17.07.2015