有人能向我解释一下如何像下面这样为顶级触发器编写测试类吗?
trigger LeadAssignmentTrigger on Broker__c (before insert,before update)
{
List<Broker__c > leadsToUpdate = new List<Broker__c >();
for (Broker__c broker: Trigger.new)
{
if (broker.Referral_ID__c!= NULL)
{
String str = broker.Referral_ID__c;
Integer ln = str.Length();
String likeStr = '%'+str.subString(ln-10, ln-7)+'%'+str.subString(ln-7, ln-4) +'%'+ str.subString(ln-4);
// Find the sales rep for the current zip code
List<User> zip = [select Id from User
where MobilePhone Like : likeStr];
// if you found one
if (zip.size() > 0)
{
//assign the lead owner to the zip code owner
broker.OwnerId = zip[0].Id;
leadsToUpdate.add(broker);
}
else
{
// Throw Error
broker.addError('Invalid Referrel ID');
}
}
}
}我是salesforce.Anyone的新手,帮助我为上面的触发器编写顶级类(测试类)。
@isTest
private class LeadAssignmentTriggerTest
{
static testMethod void validateHelloWorld()
{
User userObj = new User( Id = UserInfo.getUserId() );
userObj.MobilePhone = '5555555555';
update userObj
test.startTest();
try
{
Broker__c broker = new Broker__c();
broker.Referral_ID__c = '55555555';
broker.City ='New York';
// Add all required field here
insert broker;
}
Catch(Exception ee)
{
}
test.stopTest();
}
}AccountBrowseExtensionTesttestAccountBrowseSystem.DmlException:插入失败。第一行上的第一个异常;第一个错误: Class.AccountBrowseExtensionTest.testAccountBrowse:,City是强制性的:[]堆栈跟踪: CloseActivityControllerTesttestCloseActivitySystem.DmlException:第20行,第1列CloseActivityControllerTesttestCloseActivitySystem.DmlException:插入失败。第一行上的第一个异常;第一个错误: Class.CloseActivityControllerTest.testCloseActivity:,City是强制性的:[]堆栈跟踪: changeOwnerControllerTesttestchangeOwnerSystem.DmlException:第13行,第1列changeOwnerControllerTesttestchangeOwnerSystem.DmlException:插入失败。第一行上的第一个异常;第一个错误: Class.changeOwnerControllerTest.testchangeOwner:,City是强制性的:[]堆栈跟踪: cntactsclassTesttestcntactsSystem.DmlException:第20行,第1列cntactsclassTesttestcntactsSystem.DmlException:插入失败。第一行上的第一个异常;第一个错误: FIELD_CUSTOM_VALIDATION_EXCEPTION,FIELD_CUSTOM_VALIDATION_EXCEPTION是强制性的:[]堆栈跟踪: Class.cntactsclassTest.testcntacts:第13行,第1列FIELD_CUSTOM_VALIDATION_EXCEPTION插入失败。第一行上的第一个异常;第一个错误: FIELD_CUSTOM_VALIDATION_EXCEPTION,FIELD_CUSTOM_VALIDATION_EXCEPTION是强制性的:[]堆栈跟踪: Class.LogACallControllerTest.testLogACall:第14行,第1列FIELD_CUSTOM_VALIDATION_EXCEPTION插入失败。第一行上的第一个异常;第一个错误: FIELD_CUSTOM_VALIDATION_EXCEPTION,City是强制性的:[]堆栈跟踪: Class.RedirectControllerTest.testRedirect:第20行,第1列TestAccountSharetestSystem.DmlException: Insert failed。行0上的第一个异常;第一个错误: FIELD_CUSTOM_VALIDATION_EXCEPTION,指定固定后移动号码是强制性的。:[]堆栈跟踪: Class.TestAccountShare.test:第40行,第1列
发布于 2016-03-29 20:54:02
您正在为Broker__c在before insert和before update上编写触发器
因此,由于它是一个触发器,您的代码将在每次插入或更新记录时运行。
要编写测试类,只需创建两个测试方法:
检查这里如何创建测试类
顺便说一下,您应该检查一下关于如何编写更好的触发器和处理程序这里的最佳编码实践。
编辑:您还应该删除循环中的SOQL,并在for循环之外创建一个Map和查询
https://stackoverflow.com/questions/36285526
复制相似问题