最小起订量来设置一个函数返回基于称为次

问题描述:

我需要模拟一个接口来调用到MSMQ,是有办法,我可以用最小起订量来模拟真实的MSMQ情况下,有队列中的10条消息,我打电话嘲笑功能的10倍,我可以得到一个预先定义的对象,在第11次我应该得到一个不同的返回值(例如NULL)?

I need to mock an interface to call to MSMQ, is there a way I can use Moq to simulate real MSMQ scenario that there are 10 messages in the queue, I call mocked function 10 times and I can get a pre-defined object, on 11th time I should get a different return value (e.g. null)?

起订量现在有一个延长方法名为 SetupSequence()起订量命名空间,这意味着你可以为每个定义不同的返回值特定呼叫。

Moq now has an extension method called SetupSequence() in the Moq namespace which means you can define a distinct return value for each specific call.

总的想法是,你刚才链中的返回值你的需要。
在波纹管第一次调用的例子将返回并第二次调用将返回

The general idea is that that you just chain the return values you need. In the example bellow the first call will return Joe and the second call will return Jane:

customerService
.SetupSequence(s => s.GetCustomerName(It.IsAny<int>()))
.Returns("Joe")   //first call
.Returns("Jane"); //second call



一些更多的信息的这里