简体   繁体   English

用于APEX触发器的Salesforce测试类

[英]Salesforce Test Class for APEX Trigger

I'm in need of some help regarding the writing of test script that covers enough of the below trigger that I have managed to get working on my Sandbox account. 我需要一些有关编写测试脚本的帮助,该脚本涵盖了以下足以触发我的Sandbox帐户的触发器。 The trigger is to create extra assets when certain types of Opportunities are closed. 触发是在关闭某些类型的机会时创建额外的资产。 The Trigger seems to run fine but I really have no idea how to start writing test cases... In order for these Opportunities to close, the Account needs to have the following completed (I've included some example data - They are picklists so need to be specif amounts): 触发器似乎运行良好,但我真的不知道如何开始编写测试用例...为了关闭这些机会,该帐户需要完成以下操作(我已经提供了一些示例数据-它们是选择列表,因此必须是具体金额):

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

Trigger as follows: 触发如下:

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;
    }
  }
}

If anyone could help me out on this I would be grateful! 如果有人可以帮助我,我将不胜感激!

Thanks 谢谢

ETA test script for this trigger so far: 到目前为止,此触发器的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;
  }        
}

If anyone could say if I am going in the right direction with this I would be grateful. 如果有人能说出我是否朝着正确的方向前进,我将不胜感激。 Attempting to iron out the errors but there is obviously no point if this is not going to work anyway. 试图消除错误,但是如果这样做仍然无法解决,显然是没有意义的。 Thanks 谢谢

Here is a link to the Apex code documentation that shows how to create a test. 这是指向Apex代码文档的链接,该文档显示了如何创建测试。

All you need to do is write a testMethod that inserts or updates an Opportunity whilst meeting the criteria you define in your trigger. 您所需要做的就是编写一个testMethod,该插入或更新机会同时满足您在触发器中定义的条件。 A good unit test should test various sceneries and verify that the code produces the expected outputs (in this case, query the new Asset). 一个好的单元测试应该测试各种场景,并验证代码是否产生了预期的输出(在这种情况下,请查询新的资产)。

Also, I should point out that your code has a serious flaw in it's design. 另外,我应该指出,您的代码在设计上存在严重缺陷。 You should almost never have DML statements (or any database statements for that matter) inside of a loop. 您几乎永远不要在循环内包含DML语句(或与此有关的任何数据库语句)。 I have provided you with a fixed version of your code but I strongly suggest you head over to developer.force.com and follow some of the getting started material to avoid future headaches. 我已经为您提供了代码的固定版本,但我强烈建议您转到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;
}

First of all - your trigger has the trouble on implementation, because it isn't BULK. 首先-您的触发器在实现上遇到了麻烦,因为它不是大容量的。 Read the following articles for more details: 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/ 请阅读以下文章以了解更多详细信息: 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/

The main trouble is using DML operation in the for loop. 主要的麻烦是在for循环中使用DML操作。

Regarding a testing process for this code I think that the best way is using the following scheme: 关于此代码的测试过程,我认为最好的方法是使用以下方案:

you should to test all possible behavior on your code, and negative scenarios should be covered as well as positive. 您应该测试代码上所有可能的行为,否定方案应包括正向方案。 Thus 从而

 @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.
      }
 }

Hope this might be helpful for you 希望这对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM