首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为先端触发器编写测试类

如何为先端触发器编写测试类
EN

Stack Overflow用户
提问于 2016-03-29 13:19:53
回答 1查看 1K关注 0票数 1

有人能向我解释一下如何像下面这样为顶级触发器编写测试类吗?

代码语言:javascript
复制
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的新手,帮助我为上面的触发器编写顶级类(测试类)。

代码语言:javascript
复制
@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:插入失败。第一行上的第一个异常;第一个错误: Class.AccountBrowseExtensionTest.testAccountBrowse:,City是强制性的:[]堆栈跟踪: CloseActivityControllerTesttestCloseActivitySystem.DmlException:第20行,第1列CloseActivityControllerTesttestCloseActivitySystem.DmlException:插入失败。第一行上的第一个异常;第一个错误: Class.CloseActivityControllerTest.testCloseActivity:,City是强制性的:[]堆栈跟踪: changeOwnerControllerTesttestchangeOwnerSystem.DmlException:第13行,第1列changeOwnerControllerTesttestchangeOwnerSystem.DmlException:插入失败。第一行上的第一个异常;第一个错误: Class.changeOwnerControllerTest.testchangeOwner:,City是强制性的:[]堆栈跟踪: cntactsclassTesttestcntactsSystem.DmlException:第20行,第1列cntactsclassTesttestcntactsSystem.DmlException:插入失败。第一行上的第一个异常;第一个错误: FIELD_CUSTOM_VALIDATION_EXCEPTION,FIELD_CUSTOM_VALIDATION_EXCEPTION是强制性的:[]堆栈跟踪: Class.cntactsclassTest.testcntacts:第13行,第1列FIELD_CUSTOM_VALIDATION_EXCEPTION插入失败。第一行上的第一个异常;第一个错误: FIELD_CUSTOM_VALIDATION_EXCEPTION,FIELD_CUSTOM_VALIDATION_EXCEPTION是强制性的:[]堆栈跟踪: Class.LogACallControllerTest.testLogACall:第14行,第1列FIELD_CUSTOM_VALIDATION_EXCEPTION插入失败。第一行上的第一个异常;第一个错误: FIELD_CUSTOM_VALIDATION_EXCEPTION,City是强制性的:[]堆栈跟踪: Class.RedirectControllerTest.testRedirect:第20行,第1列TestAccountSharetestSystem.DmlException: Insert failed。行0上的第一个异常;第一个错误: FIELD_CUSTOM_VALIDATION_EXCEPTION,指定固定后移动号码是强制性的。:[]堆栈跟踪: Class.TestAccountShare.test:第40行,第1列

EN

回答 1

Stack Overflow用户

发布于 2016-03-29 20:54:02

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

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

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

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

检查这里如何创建测试类

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36285526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档