SetupSequence in Moq

Florian picture Florian · Jul 3, 2012 · Viewed 33.7k times · Source

I want a mock returns a 0 the first time, then returns 1 anytime the method was called. The problem is that if the method is called 4 times, I should write that :

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 the next times the method was called after the first time, the method returns 1 ? Thank you

Is it good to have more "operators" for SetupSequence ? If you think YES you can vote : http://moq.uservoice.com/forums/11304-general/suggestions/2973521-setupsequence-more-operators

Answer

Jakub Konecki picture Jakub Konecki · Jul 3, 2012

The cleanest way is to create a Queue and pass .Dequeue method to Returns

.Returns(new Queue<int>(new[] { 0, 1, 1, 1 }).Dequeue);