I want a mock that returns 0
the first time, then returns 1
anytime the method is called thereafter. The problem is that if the method is called 4 times, I have to write:
mock.SetupSequence(x => x.GetNumber()) .Returns(0) .Returns(1) .Returns(1) .Returns(1);
Otherwise, the method returns null.
Is there any way to write that, after the initial call, the method returns 1
?
Verifiable(); 'Setup' mocks a method and 'Returns' specify what the mocked method should return. 'Verifiable' marks this expectation to verified at the end when Verify or VerifyAll is called i.e. whether AddIncomePeriod was called with an object of IncomePeriod and if it returned the same output.
The cleanest way is to create a Queue
and pass .Dequeue
method to Returns
.Returns(new Queue<int>(new[] { 0, 1, 1, 1 }).Dequeue);
That's not particulary fancy, but I think it would work:
var firstTime = true; mock.Setup(x => x.GetNumber()) .Returns(()=> { if(!firstTime) return 1; firstTime = false; return 0; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With