2013-07-02 59 views
0

任何人都可以幫助我爲以下班級創建測試方法。此代碼的基本思想是用所有子元素克隆專利,並更改日期。爲班級創建測試方法 - 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

回答

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

感謝Shimshon!它的工作 – user2543669

+0

@ shimshon-korits你可以分享這個測試課程的方法嗎?爲什麼克隆的領域沒有被考慮? – MnZ