2012-10-03 71 views
0

我是一個完整的代碼noob,需要幫助編寫Salesforce中觸發器的測試類。任何幫助將不勝感激。在Salesforce中的觸發器上編寫測試類

這裏是觸發:

trigger UpdateWonAccounts on Opportunity(before Update) { 
    Set <Id> accountIds = new Set <Id>(); 

    //Collect End user Ids which has won Opportunities 
    for (Opportunity o : Trigger.new) { 
    if (o.isWon && o.EndUserAccountName__c != null) { 
     accountIds.add(o.EndUserAccountName__c); 
    } 
    } 

    List <Account> lstAccount = new List <Account>(); 

    //Iterate and collect all the end user records 
    for (Account a : [Select Id, Status__c From Account where Id IN : accountIds]) { 
    lstAccount.add(new Account(Id = a.Id, Status__c = true)); 
    } 

    //If there are any accounts then update the records 
    if (!lstAccount.isEmpty()) { 
    update lstAccount; 
    } 
} 
+1

你需要編寫代碼來測試這個特定的觸發幫助嗎?或者你一般需要幫助創建單元測試課程? – slashingweapon

回答

1

閱讀An Introduction to Apex Code Test MethodsHow To Write A Trigger Test

基本上,你想要創建一個新的測試方法,更新(插入,刪除,取消刪除,取決於你的觸發條件等)的記錄或sObject。

它看起來有點像這樣:

public class myClass { 
    static testMethod void myTest() { 
     // Add test method logic to insert and update a new Opportunity here 
    } 
}