【问题标题】:How to write a test class for an apex trigger如何为顶点触发器编写测试类
【发布时间】:2016-07-17 01:56:30
【问题描述】:

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

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的新手。任何人都可以帮助我为上述触发器编写顶点类(测试类)。

@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 列

【问题讨论】:

    标签: salesforce apex


    【解决方案1】:

    您正在为before insertbefore update 上的Broker__c 编写触发器

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

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

    1. 插入一个 Broker__c 记录
    2. 用于更新 Broker__c 记录

    查看here 了解如何创建测试类

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

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

    【讨论】:

    • ,如何创建测试类,我不知道apex类。
    • 我在上面添加了一个如何创建测试类的链接,那里很详细
    • 嗨 raym0nd,我创建了在沙箱中运行良好的测试类。当 imove 到生产时,它显示上述错误
    • @SakthiS 似乎你有一个 custom validation rules 阻止你插入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多