Salesforce中的触发器测试类

时间:2018-05-21 20:21:30

标签: salesforce apex

美好的一天每个人,

我正在尝试为我帮助编写的触发器编写测试类。触发器使用名为trigger_help__c的字段,从添加机会类型和帐户ID派生的公式字段,如果在过去90天内在该帐户上创建了该类型的商机,则在插入之前触发。除非配置文件是系统管理员。这是我的触发器:



trigger leadDuplicatePreventer on opportunity(before insert) {
   set<string> settgs = new set<string>();
   list<opportunity> opps = [select id,Trigger_Help__c  from opportunity WHERE CreatedDate = LAST_N_DAYS:90];
   Profile p=[SELECT ID, Name FROM Profile WHERE Id=:userinfo.getProfileId() Limit 1];
   for(opportunity opp : opps){
     if(opp.Trigger_Help__c != null && p.Name <> 'System Administrator'){
	 settgs.add(opp.Trigger_Help__c);
	 }
   }
   
   for(opportunity op : trigger.new){
      if(settgs.contains(op.Trigger_Help__c)){
	     op.adderror('An Opportunity of this type already exists on this Account.  Please contact a system administrator with questions.');
	  }
   
   }


   
}​
&#13;
&#13;
&#13;

我在编写测试课时遇到了麻烦,而且我一如既往地无能为力。我写了以下内容,但我对实际需要做的事情感到迷茫:

&#13;
&#13;
@isTest
private class TestleadDuplicatePreventer {
    @isTest static void TestleadDuplicatePreventerwithOneOpp() {
        Account acct = new Account(Name='Test Account');
        insert acct;
        Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                         StageName='Open Opportunity',
                                         CloseDate=System.today().addMonths(1),
                                         Facility__c='Tacoma WA',
                                         Oppty_Type__c='UCO Service',
                                         AccountID=acct.Id);
        insert opp;
        Test.startTest();
        opp= new Opportunity(Name='Opportunity Test',
                            StageName='Open Opportunity',
                            CloseDate=System.today().addMonths(2),
                            Facility__c='Tacoma WA',
                            Oppty_Type__c='UCO Service',
                            Account=[SELECT ID from Account WHERE Account.Name='Test Account']);
        try
        {
            insert opp;
        }
        catch(Exception duplicate)
        {       
        }
        Test.stopTest();
    }

}​
&#13;
&#13;
&#13;

欢迎任何和所有帮助!!

1 个答案:

答案 0 :(得分:0)

不确定您对项目的要求是什么,您可以在没有代码执行汇总和字段的情况下完成此操作,该字段计算与 Trigger_Help__c 中使用的类型的帐户相关联的机会然后对 ISNEW()的机会进行验证,如果 Account.Count_Of_Type__c&gt; 0 导致验证触发OR或机会创建一个唯一的隐藏字段,该字段是帐户ID和机会类型的串联,可由工作流程或流程构建器设置。这会阻止为给定帐户添加重复类型。

但是对于您的原始问题:在您的测试中,我没有看到您设置 Trigger_Help__c 字段,除非由自动设置。另外,在创建第二个opp时,您不需要进行帐户查询,只需使用代码中前面的 acct.Id 即可。

对于你的catch你需要断言e.getMessage()是你在触发器中添加错误所期望的消息。

您可能还需要单独执行以下操作(admin_user),其中admin_user是您使用系统管理员配置文件创建的用户,以确保在以管理员用户身份运行时不会出现错误, :

@isTest
private class TestleadDuplicatePreventer {
    @isTest static void TestleadDuplicatePreventerwithOneOpp() {
    Account acct = new Account(Name='Test Account');
    insert acct;
    Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                     StageName='Open Opportunity',
                                     CloseDate=System.today().addMonths(1),
                                     Facility__c='Tacoma WA',
                                     Oppty_Type__c='UCO Service',
                                     AccountID=acct.Id);
    insert opp;
    Test.startTest();
    opp= new Opportunity(Name='Opportunity Test',
                        StageName='Open Opportunity',
                        CloseDate=System.today().addMonths(2),
                        Facility__c='Tacoma WA',
                        Oppty_Type__c='UCO Service',
                        AccountId=acct.Id);
    try
    {
        insert opp;
    }
    catch(Exception duplicate)
    {       
       System.assertEquals('An Opportunity of this type already exists on this Account.  Please contact a system administrator with questions.', duplicate.getMessage())
    }
    //this should probably be in a separate test method
    Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
  User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com',
     EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
     LocaleSidKey='en_US', ProfileId = p.Id,
     TimeZoneSidKey='America/Los_Angeles', UserName='newuser@testorg.com');

  System.runAs(u2) {
      insert opp;
  }
    Test.stopTest();
}

}