【问题标题】:Trigger test class触发测试类
【发布时间】:2012-07-26 22:08:43
【问题描述】:

我正在为我的触发器编写一个测试类,用于检查帐户是否重复。 但是我的测试类中出现以下错误:

错误:编译错误:比较参数必须是兼容类型:Schema.SObjectField,第 35 行第 42 列的字符串

测试类是:

@isTest

public class trg_AccountDuplicatePreventer_FinalTest{
    static testMethod void Test0_TestInsertWithValue()
    {

        //Set<Account> Accset = new Set<Account>();

        Account acc1 =  new Account(Name = 'Agency0', Phone='9811309977',Physical_Street__c = 'ABC0', Physical_State_Province__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
        Account acc2 =  new Account(Name = 'Agency00', Phone='9811309988',Physical_Street__c = 'ABC00', Physical_State_Province__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
        Account acc3 =  new Account(Name = 'Agency000', Phone='9811309999',Physical_Street__c = 'ABC000', Physical_State_Province__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

        Account[] accs = new Account[]{acc1,acc2,acc3};
        insert accs;

        acc2.Phone='9811309999';
        acc3.Physical_Street__c='ABC0000';
        acc3.Phone='9811308888';
        update accs;

        Account dupe1 =  new Account(Name = 'Agency0', Phone='9811309977',Physical_Street__c = 'ABC0', Physical_State_Province__c = 'NY',Physical_Zip_Postal_Code__c =    '2010',Physical_Country__c= 'USA');


        try{
            insert dupe1;
            System.assert(false);
        }catch(DMLException e)
        {
            for (Integer i = 0; i < e.getNumDml(); i++)
            {
                 System.assert(e.getNumDml() == 1);
                 System.assert(e.getDmlIndex(i) == 0);
                 System.assert(e.getDmlFields(i).size() == 3);
                 System.assert(e.getDmlFields(i)[0] == 'Name');
                 System.assert(e.getDmlFields(i)[1] == 'Phone');
                 System.assert(e.getDmlFields(i)[2] == 'Physical_Street__c');
                 System.assert(e.getDmlMessage(i).indexOf('An account with this name, phone, street already exists.') > -1);
            }
        }
    }
}

如何在我的测试代码中修复这个错误?

谢谢!

【问题讨论】:

  • 您可能想要指定更多详细信息,例如您要在此处实现的目标。另外,您能否突出显示失败的行。
  • 您好 Anup,我的触发代码尝试根据帐户名称、街道和电话匹配或姓名和电话匹配或姓名和街道匹配来识别系统中的帐户重复项。我编写的测试类给了我 100% 的覆盖率,但我得到 1 次测试失败:
  • 消息:System.DmlException:插入失败。第 0 行的第一个异常;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,
    系统中存在具有此名称/电话/实体街道的机构记录。如果您希望创建代理记录,请将“创建新记录”的字段值更改为“是”。

    潜在的重复代理包括:
    名称、电话和实体街道匹配的代理:cs14.salesforce.com/001c0000003ncYmAAI>Agency000</a> |
    名称和电话匹配的代理机构:cs14.salesforce.com/001c0000003ncYmAAI>Agency000</a>
  • 堆栈跟踪:Class.trg_AccountDuplicatePreventer_FinalTest.Test0_TestInsertWithValue:第 14 行,第 1 列

标签: salesforce apex-code


【解决方案1】:

getDmlFields 返回 Schema.sObjectField 对象的列表,因此您需要将它们与其他 Schema.sObjectFields 进行比较,或者获取它们的名称以将它们与字符串进行比较。

System.assert(e.getDmlFields(i)[0] == Account.Name);
System.assert(e.getDmlFields(i)[1] == Account.Phone);
System.assert(e.getDmlFields(i)[2] == Account.Physical_Street__c);

【讨论】:

  • 如果我使用上述方法签名不正确,我会收到错误消息。
  • 我忘记了你只能从描述结果的字段中获取名称,而不是 schema.field。我已经修复了答案中的代码。
猜你喜欢
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 2015-01-20
  • 2019-07-20
  • 1970-01-01
相关资源
最近更新 更多