为类创建测试方法 - Force.com

时间:2013-07-02 17:34:08

标签: testing methods apex-code force.com

任何人都可以帮我为下面的类创建一个测试方法。此代码的基本思想是使用所有子元素克隆专利,只需更改日期。

公共课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);

}

我真的很感激帮助。

由于

Sylvio

1 个答案:

答案 0 :(得分:3)

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