Why when I am trying to use a mock like this:
public void Test(string param=null){
MyMock.Setup(x=>x.foo(param ?? It.IsAny<string>));
}
This obviously works fine
public void Test(string param=null){
if(param==null)
MyMock.Setup(x=>x.foo(It.IsAny<string>));
else
MyMock.Setup(x=>x.foo(param));
}
But why is that? the "param?? It.IsAny" returns param or It.IsAny what am I missing here?
When setting up Mock in Moq you actually pass not a value of function parameter (say, of tpye T
), but an Expression<T>
, which is then analyzed by Moq
.
So, when you pass value Moq
will analyze it and setup expectation for method call with that value.
When you pass It.IsAny<T>
Moq
will again analyze it and setup expectation without any arguments.
But when you pass your param?? It.IsAny<string>
param?? It.IsAny<string>
Moq
simply doesn't know how to analyze it.
MyMock.Setup(x=>x.foo(param==null ? It.IsAny<string>:param));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.