如何为顶点触发器编写测试类

时间:2016-03-29 13:19:53

标签: salesforce apex

有人可以向我解释如何编写像以下那样的顶点触发器的测试类吗?

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的新手帮助我如何为上面的触发器编写apex类(测试类)。

@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,城市是强制性的:[] 堆栈跟踪:Class.AccountBrowseExtensionTest.testAccountBrowse:第20行,第1列 CloseActivityControllerTesttestCloseActivitySystem.DmlException:插入失败。第0行的第一个例外;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,城市是强制性的:[] 堆栈跟踪:Class.CloseActivityControllerTest.testCloseActivity:第13行,第1列 changeOwnerControllerTesttestchangeOwnerSystem.DmlException:插入失败。第0行的第一个例外;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,城市是强制性的:[] 堆栈跟踪:Class.changeOwnerControllerTest.testchangeOwner:第20行,第1列 cntactsclassTesttestcntactsSystem.DmlException:插入失败。第0行的第一个例外;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,城市是强制性的:[] 堆栈跟踪:Class.cntactsclassTest.testcntacts:第13行,第1列 LogACallControllerTesttestLogACallSystem.DmlException:插入失败。第0行的第一个例外;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,城市是强制性的:[] 堆栈跟踪:Class.LogACallControllerTest.testLogACall:第14行,第1列 RedirectControllerTesttestRedirectSystem.DmlException:插入失败。第0行的第一个例外;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,城市是强制性的:[] 堆栈跟踪:Class.RedirectControllerTest.testRedirect:第20行,第1列 TestAccountSharetestSystem.DmlException:插入失败。第0行的第一个例外;第一个错误:FIELD_CUSTOM_VALIDATION_EXCEPTION,在约会被修复后必须提供手机号码。[] 堆栈跟踪:Class.TestAccountShare.test:第40行,第1列

1 个答案:

答案 0 :(得分:1)

您正在before insertbefore update

上编写Broker__c的触发器

因此,由于它是一个触发器,因此每次插入或更新记录时都会运行代码。

要编写测试类,只需创建两个测试方法:

  1. 一个插入Broker__c记录
  2. 一个更新Broker__c记录
  3. 检查here如何创建测试类

    顺便说一下,您应该检查有关如何编写更好的触发器和处理程序的最佳编码实践here

    编辑: 您还应该删除循环内的SOQL,并在for循环外创建一个包含查询的Map

相关问题