質問

Can anyone help me to create a test method for the following class. The basic idea of this code is to clone a patent with all child elements and just change the date.

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

}

I really appreciate the help.

Thanks

Sylvio

役に立ちましたか?

解決

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();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top