2012-10-03 63 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 
    } 
}