What is the difference between passing It.IsAny() and the value of It.IsAny() to a method setup

It.IsAny only allows Moq to match future invocations of method calls if used within the Setup construct. When Setup is called Moq just adds the method call to its cache of already-set-up method calls. Note that the argument to Setup in your example has type Expression<Func<IFoo, bool>>. Since you are passing in an Expression, the actual method call is not invoked and Moq has the ability to traverse the expression to figure out which parameters of the method call were explicit, and which are It.IsAny arguments. It uses this ability to determine if a future method call at runtime matches one of the already-set-up method calls.

In order to make it so that the method Bar can accept argument It.IsAny<int>(), it is necessary to make It.IsAny<int>() return an int (since that is the type of the parameter of Bar). In general, the return type of It.IsAny<T> must be T. An arbitrary value of T must be chosen. The most natural choice is default(T), which works for reference types and value types. (Read more about the default keyword here). In your case, that is default(int), which is 0.

So when you actually evaluate It.IsAny<int>() the value of 0 is immediately returned. However, when you use It.IsAny<int>() in an Expression (as in the argument to the Setup method), then the tree structure of the method call is preserved and Moq can match future method invocations to the method call encapsulated by the Expression.

So, although you cannot keep It.IsAny<int>() as a variable in any meaningful way, you can keep the entire Expression in a variable:

Expression<Func<IFoo, bool>> myExpr = x => x.Bar2(It.IsAny<int>());
mock.Setup(myExpr).Returns(true);
Assert.IsTrue(mock.Object.Bar2(123));  

Finally, I just want to remind you that Moq is open source. The source is available here. I find it valuable to have that source code so that I can click around and explore the code and the unit tests.

Leave a Comment