【问题标题】:Create a test method to a class - Force.com为类创建测试方法 - Force.com
【发布时间】:2013-07-02 17:34:08
【问题描述】:

谁能帮我为下面的类创建一个测试方法。此代码的基本思想是克隆具有所有子元素的专利并更改日期。

公开课 NovoDia {

//added an instance varaible for the standard controller
private ApexPages.StandardController controller {get; set;}
 // add the instance for the variables being passed by id on the url
private Itinerario_Diario__c po {get;set;}
private Itinerario_Diario__c pi {get;set;}
// set the id of the record that is created -- ONLY USED BY THE TEST CLASS
public ID newRecordId {get;set;}

// initialize the controller
public NovoDia(ApexPages.StandardController controller) {

    //initialize the stanrdard controller
    this.controller = controller;
    // load the current record
    po = (Itinerario_Diario__c)controller.getRecord();

   }

// method called from the VF's action attribute to clone the po
public PageReference cloneWithItems() {


     // setup the save point for rollback
     Savepoint sp = Database.setSavepoint();
     Itinerario_Diario__c newPO;

     try {

          //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
         po = [select Dia__c from Itinerario_Diario__c where id = :po.id];
         newPO = po.clone(false);
         insert newPO;


         // set the id of the new po created for testing
           newRecordId = newPO.id;


         // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
        List<Viagem__c> items = new List<Viagem__c>();
         //PI.Dia__c = PO.Dia__c;
       for (Viagem__c pi : [Select Cliente__c,Inicio_planejado__c,Entrada_Sa_da_de_Turno__c,Meio_de_Solicitacao__c,Motorista__c,Rota__c,Tipo_de_Viagem__c,Turno__c,Veiculo__c  From Viagem__c p where Itinerario_Diario__c = :po.id ])  {
           Viagem__c newPI = pi.clone(false);
             newPI.Itinerario_Diario__c = newPO.id;
             //newPI.Dia__c = PO.Dia__c;
               items.add(newPI);
       }
         insert items;  

     } catch (Exception e){
         // roll everything back in case of error
        Database.rollback(sp);
        ApexPages.addMessages(e);
        return null;
     }

    return new PageReference('/'+newPO.id+'/e?retURL=%2F'+newPO.id);

}

非常感谢您的帮助。

谢谢

西尔维奥

【问题讨论】:

    标签: testing methods apex-code force.com


    【解决方案1】:
    static testmethod  void NovoDia_Test(){
         Itinerario_Diario__c itinerarioDiario= new Itinerario_Diario__c();
         insert itinerarioDiario;
         ApexPages.StandardController sc = new ApexPages.StandardController(itinerarioDiario);
         NovoDia cttr = new NovoDia (sc); 
         cttr.cloneWithItems();
    }
    

    【讨论】:

    • @shimshon-korits 你能分享一下这个测试课的方法吗?没有考虑要克隆的字段,为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多