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

0

이와 같은 상황에서는 Lacey가 제안한대로 테스트 값을 삽입하는 것과 비슷한 mock objects (or just variables simulating expected values)을 고려하십시오. 이 기술은 콜 아웃을 수행 할 때 100 % 적용 범위를 달성하는 데 필요합니다. 예를 들어 호출 순간에 테스트를 종료하기 때문입니다.

+0

대단히 감사합니다. 나는하려고 노력할 것이다. – user1139662

관련 문제