有人可以向我解釋如何爲頂點觸發器編寫測試類,如下所示:如何爲頂點觸發器編寫測試類
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:插入失敗。第0行的第一個例外;第一個錯誤:FIELD_CUSTOM_VALIDATION_EXCEPTION,城市是強制性的:[] Stack Trace:Class.AccountBrowseExtensionTest.testAccountBrowse:line 20,column 1 CloseActivityControllerTesttestCloseActivitySystem.DmlException:插入失敗。第0行的第一個例外;第一個錯誤:FIELD_CUSTOM_VALIDATION_EXCEPTION,City爲必填項:[] 堆棧跟蹤:Class.CloseActivityControllerTest.testCloseActivity:第13行,第1列 changeOwnerControllerTesttestchangeOwnerSystem.DmlException:插入失敗。第0行的第一個例外;第一個錯誤:FIELD_CUSTOM_VALIDATION_EXCEPTION,City爲必填項:[] 堆棧跟蹤:Class.changeOwnerControllerTest.testchangeOwner:第20行第1列 cntactsclassTesttestcntactsSystem.DmlException:插入失敗。第0行的第一個例外;第一個錯誤:FIELD_CUSTOM_VALIDATION_EXCEPTION,City爲必填項:[] 堆棧跟蹤:Class.cntactsclassTest.testcntacts:第13行,第1列 LogACallControllerTesttestLogACallSystem.DmlException:插入失敗。第0行的第一個例外;第一個錯誤:FIELD_CUSTOM_VALIDATION_EXCEPTION,City爲必填項:[] 堆棧跟蹤:Class.LogACallControllerTest.testLogACall:第14行,第1列 RedirectControllerTesttestRedirectSystem.DmlException:插入失敗。第0行的第一個例外;第一個錯誤:FIELD_CUSTOM_VALIDATION_EXCEPTION,City爲必填項:[] 堆棧跟蹤:Class.RedirectControllerTest.testRedirect:第20行,第1列 TestAccountSharetestSystem.DmlException:插入失敗。第0行的第一個例外;第一誤差:預約被固定。:[]後FIELD_CUSTOM_VALIDATION_EXCEPTION,移動號碼是強制性 堆棧跟蹤:Class.TestAccountShare.test:40行,第1列
,如何創建測試類,我不知道頂類查詢。 –
我在上面添加了一個如何創建測試類的鏈接,其中非常詳細。 – raym0nd
Hi raym0nd,我創建了在沙箱中正常工作的測試類。當進入製作它顯示以上錯誤 –