2012-01-13 63 views
1

試圖在測試類設置數值爲捲起彙總字段,以提高代碼覆蓋率。我該怎麼做?的Apex測試類 - 如何設置一個彙總數場測試類

public class clsPreferredIASetExt { 

    List<PreferredIA__c> preferredias; 
    public static PreferredIA__c[] tobeClosed = new PreferredIA__c[0]; 
    public static PreferredIA__c[] newPreIAs = new PreferredIA__c[0]; 
    public static PreferredIA__c loopadd; 
    public static PreferredContact__c[] contactlists = new PreferredContact__c[0]; 
    public static Account[] InvoicedAccounts = new Account[0]; 
    public static PreferredIA__c[] monkey; 

    public clspreferrediaSetExt(ApexPages.StandardSetController controller) { 
     preferredias = (List<PreferredIA__c>) controller.getSelected(); 
    } 

    public void getInitCloseInv() { 
     tobeclosed = [select id, Account__c, Account__r.id, Account__r.Name, 
          Account__r.AccountNumber, Specialist__c, 
          PreferredInvoice__c, Status__c 
         from PreferredIA__c where Status__c = 'Invoiced' limit 150]; 

     list<string> testme = new list<string>{}; 
     for(PreferredIA__c a:tobeclosed) { 
      testme.add(a.Account__r.id);  
     } 

     InvoicedAccounts = [select id, EligibleIAs__c, PreferredOverride__c, 
            Preferred_Territory__r.rep__c, LastSurveyDate__c, 
            InitialInspectionComplete__c, Program_level__c, 
            PreferredExempt__c, Account_Status__c, 
            Active_IAs__c, Last_Training__c 
          from Account where id IN :testme]; 

     Contactlists = [select id, Account__c 
          from PreferredContact__c where Account__c IN :testme]; 

     for(PreferredIA__c q:tobeclosed) { 
      q.Status__c = 'Closed'; 
     } 

     for(Account z:invoicedaccounts) { 
      /**************************************************************** 
       The following condition is where I am trying to set the z.EligibleIAs__c 
       which is a roll up count field of PreferredIA__c objects associated with 
       the account. 
      ****************************************************************/ 
      if(z.EligibleIAs__c == 0 
       && z.Program_Level__c == 'Preferred' 
       && !z.PreferredExempt__c 
       && (z.Account_Status__c == 'Active' 
        || z.Account_Status__c == 'Product Only')) { 

       loopadd = new PreferredIA__c(); 
       system.debug(z.id); 
       system.debug(z.Account_Status__c); 
       loopadd.Account__c = z.id; 

       if(z.PreferredOverride__c != null) { 
        loopadd.Specialist__c = z.PreferredOverride__c; 
       } 
       else { 
        loopadd.Specialist__c= z.Preferred_territory__r.Rep__c; 
       } 

       for(PreferredContact__c q:contactlists) { 
        if(q.Account__c == z.id) { 
         loopadd.PreferredContact__c = q.id; 
        } 
       } 

       loopadd.CreatedDate__c = Date.Today(); 
       if(z.Last_training__c != null) { 
        loopadd.DueDate__c = z.Last_Training__c.AddDays(365); 
       } 
       else { 
        loopadd.DueDate__c = Date.Today().AddDays(365); 
       } 
       loopadd.initial__c = false; 
       loopadd.Status__c = 'Unacknowledged'; 
       newPreIAs.add(loopadd); 
      } 
      z.InitialInspectionComplete__c = true; 
     } 

     try { 
      update tobeclosed; 
      update invoicedaccounts; 
      insert newPreIAs; 
     } 
     catch(system.dmlexception q) { 
      system.debug(q); 
      system.debug(invoicedaccounts); 
      system.debug(newPreIAs); 
     } 
    } 

    public void ReceivePPW() { 
     monkey = [select id, Status__c from PreferredIA__c 
        where id in :preferredias and status__c = 'Training Completed']; 

     for (PreferredIA__c m:monkey) { 
      m.status__c = 'Awaiting Invoice'; 
     } 

     update monkey; 
    } 
} 

回答

2

我實際上看不到您要寫入字段的位置 - 或者您是否將其刪除,因爲它不工作?

這且不說,得到的答案是,你不能寫的彙總彙總字段。如果您需要該字段中的值,則應該將子記錄插入父測試記錄,並使用適當的字段值,以便您的摘要字段計算一個值。

而且,我可以看到你在開始時查詢PerferredIA__c,你的測試方法不應該依賴於數據在系統中已經,您應該插入自己的記錄您的測試代碼。原因在於,如果您嘗試部署到沒有相關數據的組織,則測試將失敗,因此隨後您的部署將會失敗。

+0

非常感謝你! – user1139662 2012-01-17 20:14:39

相關問題