One area where using MoQ is confusing is when mocking successive calls to the same method of an object. For example, I was writing some tests for legacy code where I needed to fake out multiple calls to a data reader. You remember data readers, don?t you? Here?s a snippet of the code I was testing. Ignore the map method and focus on the call to reader.Read . while (reader.Read()) { yield return map(reader); } Notice that there are multiple calls to reader.Read . The first couple times, I wanted Read to return true . The last time, it should return false . And here?s the code I hoped to write to fake this using MoQ: reader.Setup(r => r.Read()).Returns( true ); reader.Setup(r => r.Read()).Returns( true ); reader.Setup(r => r.Read()).Returns...(read more) ...
Go to the complete details ...