使用MOQ进一步自定义返回值

时间:2016-05-25 17:28:40

标签: c# .net unit-testing moq

我有以下单元测试。

[TestMethod]    
    public void IfTheSecondPaymentFailsThenTheFirstPaymentShouldBeVoided()
    {
        var iPaymentMock = new Mock<IPaymentMock>();
        var paymentSpecificationResponse = new PreregisteredAccountSpec();   
        iPaymentMock.Setup(
                counter => counter.ProcessPayment
                    (
                        It.IsAny<Context>(),
                        It.IsAny<PreregisteredAccountSpec>(),  
                        It.IsAny<Guid>())
                    ).
                 Returns((Context context, PaymentSpecification spec, Guid guid) =>
                {
                    return paymentSpecificationResponse;
                }
                );
    }

如何更改测试以将paymentSpecificationResponse.Distributions [0] .Transaction.GetVendorId()更改为&#34; 1&#34;当Distributions数组是只读时。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望根据传递给它的参数更改模拟返回值。您可以使用.Returns()的重载并提供函数。

.Returns((context, spec, guid) => { //Do something with the arguments passed to the mock })