大家好,
我试图为我帮助编写的触发器编写一个测试类。触发器使用一个名为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.');
}
}
}
我在写测试课上遇到了困难,而且我和往常一样无所适从。我写了以下几封信,但我不知道我到底需要做些什么:
@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();
}
}
感谢所有的帮助!!
发布于 2018-05-22 03:43:57
不确定项目的具体需求是什么,您可能不需要执行汇总和字段就可以完成此任务,该字段将计算与Trigger_Help__c中使用的类型相关联的帐户的机会,然后对机会进行验证,当ISNEW() (如果Account.Count_Of_Type__c > 0 )导致验证触发或在机会时创建一个隐藏字段,该字段是帐户id的连接和机会类型,可以由工作流或流程构建器设置。它将防止为给定帐户添加重复类型。
但对于您最初的问题:在您的测试中,我没有看到您设置了Trigger_Help__c字段,除非这是由自动化设置的。另外,在创建第二个opp时,您不需要执行帐户查询,只需在代码的前面使用acct.Id即可。
对于捕获,您需要断言e.getMessage()是您期望从触发器中添加错误中得到的消息。
您可能还需要在runAs( 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();
}}
https://stackoverflow.com/questions/50456085
复制相似问题