【问题标题】:Salesforce - unit testing for custom fields for Account objectSalesforce - Account 对象的自定义字段的单元测试
【发布时间】:2018-12-04 11:38:02
【问题描述】:

假设在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 设置为 trueinvoice_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;                   
                    }  
                }
            }
        }
    }
}

更新后我在Account 上也有一个触发器类,但我没有更改这 3 个自定义字段的任何值。

如果我通过应用程序进行测试,似乎有两个自定义字段是根据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); 上失败

那个字段仍然是false。为什么会这样?

【问题讨论】:

    标签: salesforce


    【解决方案1】:

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

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

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

    【讨论】:

    • 试过了。我在方法头中删除了 seealldata=true,并尝试插入帐户,但由于记录类型返回空结果,它在我的 InvoiceDeliveryMethodTriggerHandler 上失败
    【解决方案2】:

    好的。我得到了它。我就是这样做的。

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

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

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

    【讨论】:

    • 测试完成后您不必删除帐户。 testSetup 确保测试类中的所有测试都使用相同的测试数据。如果您只在 testSetup 中创建一个帐户,那么您的任何测试中都只会存在一个帐户
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多