代码之家  ›  专栏  ›  技术社区  ›  Byron Sommardahl

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

  •  3
  • Byron Sommardahl  · 技术社区  · 14 年前

    我有一个方法叫做 ProcessPayment()

    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”对象(而不是域对象),将该对象映射到域对象,并将域对象传递给网关服务上的存根方法。网关服务的响应就是方法返回的响应。但是,由于我对网关服务方法的存根方式,它不关心传入的内容。因此,我似乎无法测试该方法是否正确地将请求对象映射到域对象。

    我什么时候还能坚持BDD?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Derek Greer    14 年前

    例子:

    _mockGatewayService
                .Setup(x => x.Process(Moq.It.IsAny<PaymentInfo>())
                .Callback<PaymentInfo>(paymentInfo => _paymentInfo = paymentInfo);
    
        2
  •  0
  •   Gishu    14 年前

    据我所知,
    您想测试WebService.ProcessPayment 方法;存在输入参数a到对象B的映射,对象B用作GateWayService协作器的输入。

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

    使用最小起订量It.Is 接受谓词的约束(参数要满足的测试)。实现CheckPaymentInfo以根据预期的PaymentInfo进行断言。

    e、 g.要检查Add是否作为参数传递偶数,

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