我有一个名为 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 stub 我的网关服务。
_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”对象(不是域对象),将该对象映射到域对象,并将域对象传递给网关服务上的 stub 方法。来自网关服务的响应是该方法返回的内容。但是,由于我对网关服务方法进行 stub 的方式,它并不关心传递给它的内容。结果,我似乎无法测试该方法是否正确地将请求对象映射到域对象。
我什么时候可以在这里做并且仍然坚持 BDD?
最佳答案
要检查发送到您的 IGatewayService 的对象是否正确,您可以使用回调来设置对域对象的引用。然后,您可以针对该对象的属性编写断言。
例子:
_mockGatewayService
.Setup(x => x.Process(Moq.It.IsAny<PaymentInfo>())
.Callback<PaymentInfo>(paymentInfo => _paymentInfo = paymentInfo);
关于unit-testing - 如何使用 TDD/BDD 开发输入对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3453233/