我有一个方法叫做
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?