I'm using Moq and want to create builder classes to create my mocks with preset reasonable defaults that can be overridden during test setup as needed. The approach I took uses extension methods in which I pass input parameter values and expected output. In doing so, I'm seeing different behavior in what seems to me to be semantically equivalent code: passing It.IsAny() directly in a setup vs passing the value of It.IsAny() indirectly in a setup. Example:
public interface IFoo { bool Bar(int value); bool Bar2(int value); } public class Foo : IFoo { public bool Bar(int value) { return false; } public bool Bar2(int value) { return false; } } var mock = new Mock<IFoo>(); mock.Setup(x => x.Bar(It.IsAny<int>())).Returns(true); Assert.IsTrue(mock.Object.Bar(123)); // Succeeds var myValue = It.IsAny<int>(); mock.Setup(x => x.Bar2(myValue)).Returns(true); Assert.IsTrue(mock.Object.Bar2(123)); // Fails
Both calls are equivalent (to me), yet the call to Bar2 fails assertion. Why is this?
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.
4. It. IsAny<Guid>() means that you don't care what parameter was passed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With