testing - Apex 测试类 - 触发器未触发

标签 testing triggers apex

我编写了一个 apex 触发器,用于在帐户状态更改时更新字段 (status_update_date)。

在沙盒中,如果我更改帐户的状态,status_update_date 会成功更新。

当我运行测试类时,触发器不会被调用,并且我得到的 status_update_date 为空值。有什么想法为什么不调用触发器吗?谢谢!

下面的代码在代码底部注释掉了触发器:

    @isTest
    private class UpdateStatusTest{
public static Account A;

public static String initStatus = '';
public static String finalStatus = 'Fully Active';

static testMethod void testWithAccount() {

    A = new Account(
        Status__c = initStatus,
        Name = 'TestName'
        );

    insert A;     

    test.StartTest();
    A.Status__c = finalStatus;

    update A;
    test.StopTest();
   // System.assertEquals(finalStatus, A.Status__c);
    System.assertEquals(system.today(),A.Status_Update_Date__c);

}
}



   //  trigger UpdateStatus on Account (before update) {
   //for(Account a: trigger.new){
        //If the status has changed, update the date
   //     if (trigger.oldMap.get(a.Id).Status__c != trigger.newMap.get(a.Id).Status__c) {
    //    a.Status_Update_Date__c = system.today();
    //  }
  //}
//}

最佳答案

更新后,您应该通过soql从数据库中获取此记录

a=[SELECT Id, Status__c FROM Account WHERE Id=:a.Id];
System.assertEquals(finalStatus, A.Status__c);

关于testing - Apex 测试类 - 触发器未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33219170/

相关文章:

javascript - 如何使用 Testcafe 自动化移动虚拟设备测试

salesforce - 在 Salesforce LWC 中调用 Apex 时显示加载指示器

java - 有没有办法使用 Salesforce APEX 将图像保存到 Salesforce

salesforce - FIELD_CUSTOM_VALIDATION_EXCEPTION

rest - 在端到端测试中处理第三方 API 请求

javascript - shallow 和 render in jest 有什么区别?

MYSQL 从另一个表插入值 - 触发器

mysql - 在mysql中的两个数据库之间在mysql中触发

MySQL 跨表约束

testing - 如何发送wei/eth到合约地址? (使用松露javascript测试)