触发测试类

时间:2012-07-28 12:53:25

标签: salesforce apex-code

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

  

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

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

谢谢!

1 个答案:

答案 0 :(得分: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);