问题描述
这段代码:
hub.MockedUserRepository.Setup(r => r.Update(It.IsAny<ControllUser>()))
.Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null)))
.Verifiable();
将打印
NULL = 真
所以我想用这个匹配会抓住它:
So i am thinking using this matching will catch it:
var zombieDisconnectParameterMatcher = It.Is<ControllUser>(x => x.Zombies[0].ConnectionId == null);
hub.MockedUserRepository.Setup(r => r.Update(zombieDisconnectParameterMatcher))
.Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null)))
.Verifiable();
但事实并非如此.
为什么?
推荐答案
通过查看It
的源码,和表达式树有关.我喜欢这个问题;他们可能非常令人费解.如果您想看看以下方法定义:
By looking at the source code of It
, it has to do with expression trees. I like the question; they can be quite puzzling. If you would take a look at the following method definitions:
public static TValue It.Is<TValue>(Expression<Func<TValue, bool>> match)
{
return Match<TValue>.Create(
value => match.Compile().Invoke(value),
() => It.Is<TValue>(match));
}
public static T Match.Create<T>(Predicate<T> condition, Expression<Func<T>> renderExpression)
{
// ...
return default(T);
}
如果你要执行以下行:
var zombieDisconnectParameterMatcher = It.Is<ControllUser>(x => x.Zombies[0].ConnectionId == null);
然后It.Is<ControllUser>()
会尝试调用一个名为Match.Create<ControllUser>()
的方法,返回默认的控制用户
.我假设 ControllUser
是一个类,因此 zombieDisconnectParameterMatcher
将是 null
.您应该能够使用调试器看到这一点.所以实际上你打电话的是:
Then It.Is<ControllUser>()
will try to call a method called Match.Create<ControllUser>()
, which returns the default of ControllUser
. I assume ControllUser
is a class and therefore zombieDisconnectParameterMatcher
will be null
. You should be able to see this with the debugger. So what actually you're calling is:
hub.MockedUserRepository.Setup(r => r.Update(null))
.Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null)))
.Verifiable();
当使用非空 ControllUser
执行 Update
方法时(例如来自正在测试的方法),不会触发回调.它根本不符合条件,因为它不为空.您也会看到验证失败.
When executing the Update
method with a non-null ControllUser
(from the method that is being tested for example), the callback will not trigger. It simply doesn't match the criteria since it's not null. You would see the verification fail, also.
要解决此问题,请内联 zombieDisconnectParameterMatcher
变量,或将其设为表达式类型变量(例如,Expression
Update
被调用 Zombies[0].ConnectionId == null
?').
To resolve this issue, either inline the zombieDisconnectParameterMatcher
variable, or make it an expression typed variable (eg. Expression<Func<...>>
). The latter will make sure that the code is not executed, but treated as an expression which the mock framework can reason about ('is Update
being called with Zombies[0].ConnectionId == null
?').
这篇关于Moq It.Is<>不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!