Salesforce-帐户对象的自定义字段的单元测试

时间:2018-06-25 20:13:08

标签: salesforce

假设在Account对象中,它具有3个自定义字段,分别是invoice_delivery_methodinvoice_delivery_emailinvoice_delivery_print。对于invoice_delivery_method,类型为picklist,可能的值为EmailEmail and PrintPrintOther。其余两个自定义字段为复选框,默认为未选中,即false

现在,当用户将invoice_delivery_method字段更新为Email(通过Salesforce帐户页面或通过SOQL)时,invoice_delivery_email设置为true,并且{{ 1}}设置为invoice_delivery_print

我做的方法是创建一个触发类,如下所示:

false

在处理程序类中,我执行了以下操作:

trigger InvoiceDeliveryMethodTrigger on Account (before update) {
    InvoiceDeliveryMethodTriggerHandler.handleBeforeUpdate(Trigger.new);
}

更新后,我在public class InvoiceDeliveryMethodTriggerHandler { public static void handleBeforeUpdate(Account[] accounts){ RecordType recordType = [select Id from RecordType where sobjecttype = 'Account' and Name =: MSSP_Settings__c.getOrgDefaults().Account_Record_Type__c]; for (Account account : accounts) { if(account.RecordTypeId == recordType.Id) { System.debug('Information for Account: ' + account); System.debug('Information for Invoice Delivery Method: ' + account.Invoice_Delivery_Method__c); account.Invoice_Delivery_Email__c = false; account.Invoice_Delivery_Print__c = false; String delivery_method = account.Invoice_Delivery_Method__c; System.debug('String is not blank ' + String.isNotBlank(delivery_method)); if (String.isNotBlank(delivery_method)){ if (delivery_method.equals('Email')){ account.Invoice_Delivery_Email__c = true; account.Invoice_Delivery_Print__c = false; } else if (delivery_method.equals('Email and Mail')){ account.Invoice_Delivery_Email__c = true; account.Invoice_Delivery_Print__c = true; } else if (delivery_method.equals('Mail')){ account.Invoice_Delivery_Email__c = false; account.Invoice_Delivery_Print__c = true; } } } } } } 上也有一个触发器类,但是我没有为这3个自定义字段更改任何值。

如果我通过应用程序进行测试,则似乎两个自定义字段已根据Account的值进行了更新。但是我在单元测试中遇到了问题。

这是我写的单元测试课

invoice_delivery_method

运行测试用例时,它在 @isTest private class InvoiceDeliveryMethodTest { @isTest(SeeAllData=true) static void testAccountEmailSelected(){ Account testAccount = new Account(); // populating some of the mandatory field for Account testAccount.Invoice_Delivery_Method__c = 'Other'; insert testAccount; Account acct = [Select Id, Invoice_Delivery_Method__c, Invoice_Delivery_Email__c, Invoice_Delivery_Print__c from Account where Id =: testAccount.Id]; acct.Invoice_Delivery_Method__c = 'Email'; update acct; acct = [Select Id, Invoice_Delivery_Method__c, Invoice_Delivery_Email__c, Invoice_Delivery_Print__c from Account where Id =: testAccount.Id]; System.assertEquals('Email', acct.Invoice_Delivery_Method__c); System.assert(acct.Invoice_Delivery_Email__c); System.assert(!acct.Invoice_Delivery_Print__c); delete testAccount; } } 上失败

该字段仍为System.assert(acct.Invoice_Delivery_Email__c);。为什么会这样?

2 个答案:

答案 0 :(得分:0)

首先,我认为更简单的解决方案可能是使用公式而不是触发器。

如果您仍然想使用触发路线,建议不要使用seealldata。

那是我唯一能想到的就是记录类型。创建帐户时,您可以断言记录类型ID是否就是您所期望的?

答案 1 :(得分:0)

好。我知道了。这是我的方法。

首先删除seealldata=true批注中的所有isTest

然后根据http://sfdcsrini.blogspot.com/2014/07/how-to-readcreate-record-types-in-apex.html,我用setup创建一个@testSetup方法来创建Account数据和recordType

在测试方法中,我选择在setup()中创建的帐户,更改invoice_delivery_method的值并更新该帐户。然后再次重新选择,并进行断言检查。一旦所有断言检查都通过,请删除该帐户对象,以便仅保留一个帐户对象用于下一个测试方法。