Salesforce测试失败但在系统上工作

时间:2012-07-26 15:58:43

标签: salesforce

我已经为Saleforce触发器编写了一个测试。每当“Account_Status__c”发生变化时,触发器会将Account_Status_Change_Date__c值更改为当前日期。

我通过Web 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();
        }
    }
}

1 个答案:

答案 0 :(得分: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);
}