2013-07-08 189 views
1

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

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 = '[email protected]'; 
      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; 
    }   
} 

如果任何人都可以说,如果我在正确的方向这是怎么回事我将不胜感激。试图消除这些错误,但是如果这不起作用,显然没有意义。 感谢

回答

0

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

感谢您的快速反应!我现在已经改变了我的触发器代码,并且确保从现在开始不会在循环中包含任何DML ...关于测试,我有以下几点 - 任何人都可以说如果我要朝着正确的方向前进?我发现DeveloperForce页面上的例子有点令人困惑。非常感谢。 –

+0

在对您的测试示例进行快速浏览之后,我可以说您绝对需要浏览developer.force.com提供的教程和指南。 例如;你的测试代码没有assert语句,没有他们你确认你的代码没有未捕获的异常,但是你没有确认它是否按预期工作。在测试结束时您还会有不必要的DML删除语句,对测试代码中对象的更改不会提交给数据库。 –

0

首先 - 您的触发器在执行时遇到了麻烦,因为它不是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/

主要的麻烦是在使用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. 
     } 
} 

希望这也许对你有所帮助

+0

感谢您的快速响应!我现在已经改变了我的触发器代码,并且确保不会在现在的循环中包含任何DML ...关于测试脚本,我已经添加了我的原始问题 - 任何人都可以说我是走向正确的方向?我发现DeveloperForce页面上的例子有点令人困惑。非常感谢。 –

相关问题