2012-01-13 37 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

相关问题