【问题标题】:Salesforce Test Class for APEX TriggerAPEX 触发器的 Salesforce 测试类
【发布时间】:2013-07-05 13:10:00
【问题描述】:

我需要一些关于编写测试脚本的帮助,该脚本涵盖了我设法在我的沙盒帐户上工作的足够多的以下触发器。 触发器是在某些类型的机会关闭时创建额外的资产。触发器似乎运行良好,但我真的不知道如何开始编写测试用例......为了关闭这些机会,帐户需要完成以下内容(我已经包含了一些示例数据 - 它们是选项列表,所以需要指定数量):

a.TurnoverBand__c = '<£10 million';
a.Joining_Fee__c = '£1,920';
a.Annual_Subscription__c = '£1,320';

触发如下:

trigger CreateInclusiveAssetonMembershipWon on Opportunity (after insert, after update)
{
   for(Opportunity o: trigger.new)
  {
    if(o.isWon == true && o.HasOpportunityLineItem == true && ( o.Type == 'A Membership' || o.Type == 'AB Membership' || o.Type == 'A Membership Upgrade' || o.Type == 'AB Membership Upgrade' ) )
    {
     String opptyId = o.Id;
     Asset[] ast = new Asset[]{};
     Asset a = new Asset();
      {
      a = new Asset();
      a.AccountId = o.AccountId;
      a.Product2Id = '01tA0000003N1pW';
      a.Quantity = o.Inclusive_Training_Spaces_Allocated__c;
      a.Price = 300;
      a.PurchaseDate = o.CloseDate;
      a.Status = 'Purchased';
      a.Description = 'Allocated Spaces';
      a.Name = 'Membership Inclusive Training';
      ast.add(a);
      }
    insert ast;
    }
  }
}

如果有人能帮助我解决这个问题,我将不胜感激!

谢谢

到目前为止此触发器的 ETA 测试脚本:

@isTest
 private class TrngAstOppTrigTestSuite {

      static testMethod void verifyBehaviorOnInsert_positive() {
          Account a = new Account();
      a.Name = 'New Test Account';
      a.Account_Email__c = 'testemail4trigger@test.co.uk';
          a.TurnoverBand__c = '<£10 million';
          a.Joining_Fee__c = '£1,920';
      a.Annual_Subscription__c = '£1,320';
      insert a;

          Opportunity o = new Opportunity();
          OpportunityLineItem ol = new OpportunityLineItem();
          PricebookEntry pbID = [select ID from PricebookEntry];

      o.AccountId = a.Id;
      o.Name = 'test';
          o.Type = 'A Membership';
          o.StageName = 'Needs Analysis';
          o.CloseDate = date.today();
          insert o;

      ol.OpportunityId = o.Id;
      ol.Quantity = 1;
      ol.UnitPrice = 2.00;
          ol.PricebookEntryId = pbID.Id;

          insert ol;

      o.StageName= 'Closed Won';
          update o;

          delete ol;
          delete o;
  }        
}

如果有人能说我是否朝着正确的方向前进,我将不胜感激。试图消除错误,但如果这无论如何都行不通,显然没有意义。 谢谢

【问题讨论】:

    标签: testing triggers salesforce case apex-code


    【解决方案1】:

    Here is a link to the Apex code documentation that shows how to create a test.

    您需要做的就是编写一个插入或更新机会的 testMethod,同时满足您在触发器中定义的条件。一个好的单元测试应该测试各种场景并验证代码是否产生了预期的输出(在这种情况下,查询新的资产)。

    另外,我应该指出您的代码在其设计中存在严重缺陷。 您几乎不应该在循环中包含 DML 语句(或任何数据库语句)。我已经为您提供了代码的固定版本,但我强烈建议您前往 developer.force.com 并遵循一些入门材料以避免将来出现问题。

    trigger CreateInclusiveAssetonMembershipWon on Opportunity (after insert, after update)
    {
        Asset[] assets = new Asset[0];
        for(Opportunity o: trigger.new)
        {
            if(o.isWon == true && o.HasOpportunityLineItem == true && ( o.Type == 'A Membership' || o.Type == 'AB Membership' || o.Type == 'A Membership Upgrade' || o.Type == 'AB Membership Upgrade' ) )
            {
    
                Asset a = new Asset();
                a.AccountId = o.AccountId;
                a.Product2Id = '01tA0000003N1pW';
                a.Quantity = o.Inclusive_Training_Spaces_Allocated__c;
                a.Price = 300;
                a.PurchaseDate = o.CloseDate;
                a.Status = 'Purchased';
                a.Description = 'Allocated Spaces';
                a.Name = 'Membership Inclusive Training';
                assets.add(a);
            }
        }
        insert assets;
    }
    

    【讨论】:

    • 感谢您的快速回复!我现在已经更改了我的触发器代码,并确保从现在开始不会在循环中包含任何 DML...关于测试,我必须执行以下操作 - 有人能说我是否朝着正确的方向前进吗?我发现 DeveloperForce 页面上的示例有些令人困惑。非常感谢。
    • 快速浏览一下您的测试示例后,我可以说您肯定需要阅读 developer.force.com 提供的教程和指南。例如;您的测试代码没有断言语句,没有它们,您可以验证您的代码没有未捕获的异常,但您不能确认它是否按预期工作。您在测试结束时还有不必要的 DML 删除语句,对测试代码中对象的更改永远不会提交到数据库。
    【解决方案2】:

    首先 - 您的触发器在实现方面存在问题,因为它不是 BULK。 阅读以下文章了解更多详情: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bestpract.htm http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bulk_idioms.htm http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce/

    主要问题是在 for 循环中使用 DML 操作。

    关于此代码的测试过程,我认为最好的方法是使用以下方案:

    您应该测试代码上所有可能的行为,并且应该涵盖消极情况和积极情况。因此

     @isTest
     private class OpportunityTriggerTestSuite {
    
          static testMethod void verifyBehaviorOnInsert_positive() {
              // prepare correct opportunity and insert it
              // perform checking for opportunity and assets states
              // use System.assertEquals() or System.assert() methods
              // http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_system.htm
    
          }
    
          static testMethod void verifyBehaviorOnUpdate_positive() {
              // prepare correct opportunity and insert it
              // change a few fields on opportunity and update it
              // perform assertion for opportunity and assets
          }
    
          static testMethod void verifyBehaviorOnInsert_negative() {
              // prepare incorrect opportunity and insert it
              // perform assertion for opportunity and assets expected states/error/etc.
          }
    
          static testMethod void verifyBehaviorOnInsert_negative() {
              // prepare correct opportunity and insert it
              // check state
              // change a few fields in such manner that opportunity will be incorrect and update it
              //  perform assertion for opportunity and assets expected states/error/etc.
          }
     }
    

    希望对你有帮助

    【讨论】:

    • 感谢您的快速回复!我现在已经更改了我的触发器代码,并确保从现在开始不会在循环中包含任何 DML ......关于测试我已经得到了我在原始问题中添加的测试脚本 - 谁能说我是朝着正确的方向前进?我发现 DeveloperForce 页面上的示例有些令人困惑。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2019-06-24
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多