我有一个名为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.

要建立的背景下,我使用起订量碰伤我的网关服务。

_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?

有帮助吗?

解决方案

要检查发送到您的IGatewayService对象是正确的,你可以使用一个回调来设置域对象的引用。然后,你可以写上该对象的属性的断言。

示例:

_mockGatewayService
            .Setup(x => x.Process(Moq.It.IsAny<PaymentInfo>())
            .Callback<PaymentInfo>(paymentInfo => _paymentInfo = paymentInfo);

其他提示

所以,从我个人理解,结果 要测试在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) ));

使用Moq的It.Is约束这需要在谓词(一个用于测试的参数满足)。实施CheckPaymentInfo到与预期PaymentInfo断言。

e.g。要检查是否续传偶数作为参数,

 mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top