The sequence may have looked something like:
Call 1: mockedObject.IsValid() returns true
Call 2: mockedObject.IsValid() returns false
My solution to the problem, which is sort of outlined in the Moq Quick Start was:
[TestMethod] public void MultipleReturnValue_Test() { bool firstCall = true; var mockedObject = new Mock<ISomeInterface>(); mockedObject.Setup(o => o.IsValid()).Returns(() => { if (firstCall == true) { firstCall = false; return true; } return false; }); var sut = new MySystemUnderTest(mockedObject.Object); sut.DoSomething(); Assert.IsTrue(sut.ExpectedCondition()); }
Not beautiful, but It works fine. The first call to IsValid() returns true, while any subsequent calls return false.
This raises the question, how much logic in a test is too much?
I like this method better: http://haacked.com/archive/2009/09/29/moq-sequences.aspx
ReplyDelete