A Rather Embarrassing Reason for the "Expression references a method that does not belong to the mocked object" Exception When Using Moq
I've been using Moq for ages, but today while writing a unit test ran into a runtime exception I'd never had before:
Expression references a method that does not belong to the mocked object: mock => mock.GetExpiredCredentials(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<Boolean>()).Returns<IEnumerable`1>(.fakeResults, new[] { })
I couldn't understand why. The method in question was on the interface, and if it wasn't, the code shouldn't have even compiled, yet it did. Even more confusing was that, when I Googled the error message, all the results seemed to point toward extension methods, and this method was not an extension method.
It turns out, what I had done was to misplace my parentheses (facepalm)! Here's what my original take looked like:
Comments
Post a Comment