如何使用TDD/BDD开发输入对象?

发布于 2024-09-14 03:27:55 字数 1790 浏览 7 评论 0原文

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

I have a method called ProcessPayment() that I'm developing via BDD and mspec. I need help with a new challenge. My user story says:

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

To set up the context, I am stubbing my gateway service using Moq.

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

Here's the spec:

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>());

}

The method should take a `PaymentProcessingRequest' object (not domain obj), map that obj to a domain obj, and pass the domain obj to the stubbed method on the gateway service. The response from the gateway service is what gets returned by the method. However, because of the way I am stubbing my gateway service method, it doesn't care what gets passed in to it. As a result, it seems I have no way to test whether or not the method maps the request object to the domain object properly.

When can I do here and still adhere to BDD?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2024-09-21 03:27:55

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

例子:

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

To check that the object sent to your IGatewayService is correct, you can use a callback to set a reference to the domain object. You can then write your assertions on properties of that object.

Example:

_mockGatewayService
            .Setup(x => x.Process(Moq.It.IsAny<PaymentInfo>())
            .Callback<PaymentInfo>(paymentInfo => _paymentInfo = paymentInfo);
南冥有猫 2024-09-21 03:27:55

据我了解,
您想要测试 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 进行断言。

例如,检查 Add 是否传递了偶数作为参数,

 mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true); 

So from what I understand,
You want to test the mapping logic in the WebService.ProcessPayment method ; there is a mapping of an input parameter A to an object B, which is used as an input to a GateWayService collaborator.

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

Use the Moq It.Is constraint which takes in a Predicate (a test for the argument to satisfy). Implement CheckPaymentInfo to assert against the expected PaymentInfo.

e.g. to check if Add is Passed an even number as an argument,

 mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文