Salesforce测试失败,但正在系统上工作
我已经为Saleforce触发器编写了一个测试。每当“帐户状态”更改时,触发器将Salesforce测试失败,但正在系统上工作,salesforce,Salesforce,我已经为Saleforce触发器编写了一个测试。每当“帐户状态”更改时,触发器将帐户状态更改日期更改为当前日期 我已经通过Web GUI实际更改了帐户状态的值,并且帐户状态更改日期确实设置为当前日期。然而,我的测试代码似乎并没有触发这个触发器。我不知道人们是否知道这样做的原因 我的代码如下: @isTest public class testTgrCreditChangedStatus { public static testMethod void testAccountStatusC
帐户状态更改日期更改为当前日期
我已经通过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();
}
}
}
这样做不需要触发器。这可以通过一个简单的工作流规则来完成。但是,测试代码的问题是,您需要在更新后查询帐户,以获取帐户字段中的更新值
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);
}
非常感谢。这非常有效:)因此,只是为了确认,我测试代码中的acc
变量保存在内存中的某个地方,但实际触发器只更新数据库,因此我需要重新选择,以便将更新后的值保存到acc
变量内存中。对吗?确切地说,Sobjects存储在内存中的方式与任何其他对象相同,因此数据库中的更改不会传播回它们,除非您查询更改的数据。