0

我正在为我的触发器编写一个测试类来检查帐户重复。但是我在测试类中收到以下错误:

错误:编译错误:比较参数必须是兼容类型: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);
            }
        }
    }
}

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

谢谢!

4

1 回答 1

2

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);
于 2012-07-28T14:10:40.230 回答