2012-07-28 66 views
0

我正在为我的触发器编写一个测试类,用于检查帐户重复项。 但我得到以下错误在我的测试类:触发器测试类

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 35 column 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); 
      } 
     } 
    } 
} 

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

谢谢!

+0

您可能想要指定更多细节,例如您要在此处实现的内容。另外,你能突出显示它失败的地方吗? – Anup 2012-07-28 23:29:59

+0

嗨,Anup,我的触发器代码尝试根据帐户名称,街道和手机匹配或名称和手机匹配或名称和街道匹配来识别系统中的帐户重复。我写的测试类给了我100%的覆盖率,但是我得到1个测试失败: – user1518186 2012-07-29 08:05:20

+0

消息:System.DmlException:插入失败。第0行的第一个例外;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,
具有此名称/电话/物理街道的代理记录存在于系统中。如果您希望创建代理记录,请将“创建新记录”的字段值更改为YES。

潜在重复机构包括:
名称,电话和实体街匹配的代理商:Agency000 |
与名称和电话匹配的代理商:Agency000 user1518186 2012-07-29 08:12:34

回答

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); 
+0

我收到错误,如果我使用上述方法签名不正确。 – user1518186 2012-07-28 14:31:13

+0

请提出建议如何涵盖上述触发器..有点迫切.. – user1518186 2012-07-28 14:42:09

+1

我忘了你只能从字段描述结果而不是schema.field。我已经修复了答案中的代码。 – 2012-07-28 15:28:16