2015-07-16 1 views
2

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

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

Это мой класс apex.

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.

ответ

1

Вам необходимо настроить стандартный контроллер для вставленной возможности. Затем передайте 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); 

} 
Смежные вопросы