2016-03-29 33 views
1

有人可以向我解釋如何爲頂點觸發器編寫測試類,如下所示:如何爲頂點觸發器編寫測試類

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列

回答

1

您上before insert寫Broker__c觸發和before update

因此,因爲它是觸發器,所以每次插入或更新記錄時都會運行代碼。

編寫測試類簡單地創建了兩個測試方法:

  1. 一個插入Broker__c記錄
  2. 一個更新Broker__c記錄

檢查here關於如何創建測試類

順便說一句,你應該檢查如何編寫更好的觸發器和處理程序的最佳編碼做法here

編輯: 你也應該刪除SOQL內環路,並創建一個映射與外部for循環

+0

,如何創建測試類,我不知道頂類查詢。 –

+0

我在上面添加了一個如何創建測試類的鏈接,其中非常詳細。 – raym0nd

+0

Hi raym0nd,我創建了在沙箱中正常工作的測試類。當進入製作它顯示以上錯誤 –