2016-12-21 3 views
1

入力がいくつかのアサートを変更しても、ほぼすべてが同じままである場合、どの部分がアサート部分を再利用するか。私の例では、入力の1つが最後のアサーションだけを変更し、他のものは同じままになります。xBehaveテストでアサートやセットアップを再利用するには?

"When CreateBidCommand is executed" 
     .x(() => 
     { 
      _createBidCommand = new CreateBidCommand(_client, _bidYear, _optionNumber, 
       _underwritingLicenseFiling, _underwriter, _bidType, _description, _claimsApplicationType); 
      _commandDispatcher.Send(_createBidCommand); 
     }); 
    "Then Bid should be created" 
     .x(() => 
     { 
      _bid = _bidRepository.FindByBidNumber(_client, _bidYear, _optionNumber); 
      Assert.NotNull(_bid); 
     }); 
    " with description" 
     .x(() => Assert.Equal(_bid.Description, _description)); 
    " with Client" 
     .x(() => Assert.Equal(_client.Id, _bid.Client.Id)); 
    " with OptionNumber" 
     .x(() => Assert.Equal(_bid.OptionNumber, _optionNumber)); 
    " with BidType" 
     .x(() => { Assert.Equal(_bid.BidType.Code, _bidType.Code); }); 
    " with ClaimsApplicationType " 
     .x(() => Assert.Equal(_bid.ClaimsApplicationType.Code, _claimsApplicationType.Code)); 
    " with RegulatoryBody" 
     .x(() => Assert.Equal(_bid.RegulatoryBody,_underwritingLicenseFiling.RegulatoryBody)); 
    " with Underwriter" 
     .x(() => Assert.Equal(_bid.Underwriter, _underwriter)); 
    " with UnderwritingFirm" 
     .x(() => Assert.Equal(_bid.UnderwritingFirm,_underwritingLicenseFiling.UnderwritingFirm)); 
    "Then one and only one BidProposal should be created" 
     .x(() => Assert.True(_bid.BidProposals().Count() == 1)); 
    " with BaseForm" 
     .x(() => Assert.Equal(_underwritingLicenseFiling.BaseForm, _bid.LatestProposal().BaseForm)); 
    "Then one and only one ClientPolicy should be created" 
     .x(() => 
     { 
      var clientPolicies = _clientPolicyRepository.FindByBidId(_bid.Id); 
      Assert.Equal(clientPolicies.Count(), 1); 
     }); 
    "Then ProductionSchedule should have only one step" 
     .x(() => Assert.True(_bid.LatestProposal().ProductionSchedule.Count() == 1)); 
    " and it should be Initial creation" 
     .x(() => 
       Assert.True(_bid.LatestProposal().ProductionSchedule.ElementAt(0).BidStatusType.Code == 
          BidStatusTypeCode.InitialCreation)); 

答えて

0

これらのフィールドを異なるものにすると、意味がわかりやすいものになります。作成した入札単価と照合するために使用できるものを導入することを検討してください。ビルダー・パターンを使用して、必要なフィールドを簡単に作成することができます。例えば:ビルダーで

// (Type declared here just to make it obvious) 
FakeBid _expectedBid = new FakeBidBuilder().WithClient(_client) 
            .WithBidYear(_bidYear) 
            // etc. 
            .Build(); 

、あなたも簡単にこれだけの例外や、意味のあるデータが実際に目に見えて、ここで設定する必要があり、デフォルト値を設定することができます。

このコマンドを呼び出すと、あなたは今、この偽の入札からパラメータを渡すことができます。

_createBidCommand = new CreateBidCommand(
     _expectedBid.Client, 
     _expectedBid.BidYear, 
     // etc. 
     ); 

そして今、あなたはFakeBidクラスにマッチャを置く:

AssertTrue(_expectedBid.Matches(_bid)); 

どれでも異例のチェックを別々に確認することができます。

ビッドマッチングのさまざまな方法について具体的なことがある場合は、ビジネスや担当者と話し合い、入札単価やその意味を調べ、その言語をキャプチャできるかどうかを確認してください異なるビルダーとクラス名。

入札作成の2つの異なる側面が2つの異なるステークホルダーにとって価値がある場合は、おそらく異なる例に分かれている必要があることに注意してください。

このようなクラスレベルのサンプル/テストを行う主な理由は、バグを捕まえたりコードをテストしたりすることではありません。それはきれいなデザインを徹底させ、一緒に来て、コードが何をするのか、なぜそれが価値あるのかを理解したいと思っている人のために、生きているドキュメンテーションを提供することです。これらのパターンがあなたにいくつかのアイデアを与えることを望みます。

0

標準のアサーションを実行するテストDSLを作成することでこれを行います。 AssertBidは私のテストのDSLのメソッドである

"When CreateBidCommand is executed" 
     .x(() => 
     { 
      _createBidCommand = new CreateBidCommand(_client, _bidYear, _optionNumber, 
       _underwritingLicenseFiling, _underwriter, _bidType, _description, _claimsApplicationType); 
      _commandDispatcher.Send(_createBidCommand); 
     }); 
    "Then Bid should be created" 
     .x(() => 
     { 
      _bid = _bidRepository.FindByBidNumber(_client, _bidYear, _optionNumber); 
      Assert.NotNull(_bid); 
     }); 
    "And the bid should have the properties descibed by the command" 
     .x(() => AssertBid(_bid, _description, _client, ...)); 
    ... 

関連する問題