1

以下のトリガーを十分にカバーするテスト スクリプトの作成に関して、サンドボックス アカウントで作業することができたので、助けが必要です。トリガーは、特定のタイプの商談がクローズされたときに追加のアセットを作成することです。トリガーは正常に動作しているようですが、テスト ケースの作成方法がまったくわかりません... これらの商談をクローズするには、アカウントで次のことを完了する必要があります (いくつかのサンプル データを含めました - それらはピックリストなので、特定の金額である必要があります):

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

私がこれで正しい方向に進んでいるかどうか誰かが言うことができれば、私は感謝します. エラーを解決しようとしていますが、とにかくうまくいかない場合は明らかに意味がありません。ありがとう

4

2 に答える 2

0

テストの作成方法を示す Apex コードドキュメントへのリンクを次に示します。

必要なのは、トリガーで定義した基準を満たしながら Opportunity を挿入または更新する 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;
}
于 2013-07-08T10:42:28.257 に答える
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/

主な問題は、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.
      }
 }

これがお役に立てば幸いです

于 2013-07-08T11:46:50.503 に答える