3

ProcessPayment()BDDとmspecを介して開発しているというメソッドがあります。新しい挑戦に助けが必要です。私のユーザーストーリーは次のように述べています。

Given a payment processing context,
When payment is processed with valid payment information,
Then it should return a successful gateway response code.

コンテキストをセットアップするために、Moq を使用してゲートウェイ サービスをスタブ化しています。

_mockGatewayService = Mock<IGatewayService>();
_mockGatewayService.Setup(x => x.Process(Moq.It.IsAny<PaymentInfo>()).Returns(100);

仕様は次のとおりです。

public class when_payment_is_processed_with_valid_information {

    static WebService _webService;
    static int _responseCode;
    static Mock<IGatewayService> _mockGatewayService;
    static PaymentProcessingRequest _paymentProcessingRequest;

    Establish a_payment_processing_context = () => {

        _mockGatewayService = Mock<IGatewayService>();
        _mockGatewayService
            .Setup(x => x.Process(Moq.It.IsAny<PaymentInfo>())
            .Returns(100);

        _webService = new WebService(_mockGatewayService.Object);

        _paymentProcessingRequest = new PaymentProcessingRequest();
    };

    Because payment_is_processed_with_valid_payment_information = () => 
        _responseCode = _webService.ProcessPayment(_paymentProcessingRequest); 

    It should_return_a_successful_gateway_response_code = () => 
        _responseCode.ShouldEqual(100);

    It should_hit_the_gateway_to_process_the_payment = () => 
        _mockGatewayService.Verify(x => x.Process(Moq.It.IsAny<PaymentInfo>());

}

メソッドは「PaymentProcessingRequest」オブジェクト (ドメイン obj ではない) を受け取り、その obj をドメイン obj にマップし、ドメイン obj をゲートウェイ サービスのスタブ メソッドに渡す必要があります。ゲートウェイ サービスからの応答は、メソッドによって返されるものです。ただし、ゲートウェイ サービス メソッドをスタブ化しているため、何が渡されても問題ありません。その結果、メソッドがリクエスト オブジェクトをドメイン オブジェクトに適切にマップするかどうかをテストする方法がないようです。

ここで BDD を遵守できるのはいつですか?

4

2 に答える 2

2

IGatewayService に送信されたオブジェクトが正しいことを確認するには、コールバックを使用してドメイン オブジェクトへの参照を設定します。その後、そのオブジェクトのプロパティにアサーションを記述できます。

例:

_mockGatewayService
            .Setup(x => x.Process(Moq.It.IsAny<PaymentInfo>())
            .Callback<PaymentInfo>(paymentInfo => _paymentInfo = paymentInfo);
于 2010-08-10T21:30:16.650 に答える
0

私が理解していることから
、 WebService.ProcessPayment メソッドでマッピングロジックをテストしたいと考えています。入力パラメータ A からオブジェクト B へのマッピングがあり、GateWayService コラボレータへの入力として使用されます。

It should_hit_the_gateway_to_process_the_payment = () => 
        _mockGatewayService.Verify(
             x => x.Process( Moq.It.Is<PaymentInfo>( info => CheckPaymentInfo(info) ));

Predicate (引数が満たされるかどうかのテスト) を受け取る Moq It.Is 制約を使用します。CheckPaymentInfo を実装して、予想される PaymentInfo に対してアサートします。

たとえば、Add が引数として偶数を渡されたかどうかを確認するには、

 mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true); 
于 2010-08-11T05:14:11.233 に答える