2012-07-26 3 views
1

Salesforce 트리거 테스트를 작성했습니다. 'Account_Status__c'가 변경 될 때마다 트리거는 Account_Status_Change_Date__c 값을 현재 날짜로 변경합니다.Salesforce 테스트가 실패했지만 시스템에서 작동 중입니다.

웹 GUI를 통해 계정 상태 값을 실제로 변경했으며 계정 상태 변경 날짜가 실제로 현재 날짜로 설정되었습니다. 그러나 내 테스트 코드는이 트리거를 시작하지 않는 것 같습니다. 나는 사람들이 이것에 대한 어떤 이유를 알고 있는지 궁금해.

@isTest 
public class testTgrCreditChangedStatus { 

    public static testMethod void testAccountStatusChangeDateChanged() { 

     // Create an original date 
     Date previousDate = Date.newinstance(1960, 2, 17); 

     //Create an Account 
     Account acc = new Account(name='Test Account 1'); 
     acc.AccountNumber = '123'; 
     acc.Customer_URN_Number__c = '123'; 
     acc.Account_Status_Change_Date__c = previousDate; 
     acc.Account_Status__c = 'Good'; 
     insert acc; 

     // Update the Credit Status to a 'Bad Credit' value e.g. Legal 
     acc.Account_Status__c = 'Overdue'; 
     update acc; 

     // The trigger should have updated the change date to the current date 
     System.assertEquals(Date.today(), acc.Account_Status_Change_Date__c); 
    } 
} 

trigger tgrCreditStatusChanged on Account (before update) { 

    for(Account acc : trigger.new) { 
     String currentStatus = acc.Account_Status__c; 
     String oldStatus = Trigger.oldMap.get(acc.id).Account_Status__c; 

     // If the Account Status has changed... 
     if(currentStatus != oldStatus) { 
      acc.Account_Status_Change_Date__c = Date.today(); 
     } 
    } 
} 

답변

3

당신은이 작업을 수행하는 트리거가 필요하지 않습니다 다음과 같이

내 코드입니다. 계정에 대한 간단한 워크 플로 규칙을 사용하여 수행 할 수 있습니다. 그러나 테스트 코드 문제는 계정 필드에서 업데이트 된 값을 얻기 위해 업데이트 한 후에 계정을 쿼리해야한다는 것입니다.

public static testMethod void testAccountStatusChangeDateChanged() { 

    ... 
    insert acc; 

    Test.StartTest(); 
    // Update the Credit Status to a 'Bad Credit' value e.g. Legal 
    acc.Account_Status__c = 'Overdue'; 
    update acc; 
    Test.StopTest(); 

    acc = [select Account_status_change_date__c from account where id = :acc.id]; 
    // The trigger should have updated the change date to the current date 
    System.assertEquals(Date.today(), acc.Account_Status_Change_Date__c); 
} 
+0

대단히 감사합니다. 이것은 훌륭하게 작동합니다. :) 따라서, 테스트 코드의'acc' 변수는 어딘가에서 메모리에 저장되지만 실제 트리거는 데이터베이스 만 업데이트하므로 업데이트 된 값을 얻기 위해 다시 선택해야합니다. acc' 변수 메모리. 권리? – Joe

+1

정확히, Sobjects는 다른 개체와 같은 방식으로 메모리에 저장되므로 변경된 데이터를 쿼리하지 않으면 DB의 변경 사항이 다시 해당 개체로 전파되지 않습니다. –

+0

다시 한번 감사드립니다 :) – Joe

관련 문제