Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 最小起订量是<&燃气轮机;不匹配_C#_Unit Testing_Mocking_Moq - Fatal编程技术网

C# 最小起订量是<&燃气轮机;不匹配

C# 最小起订量是<&燃气轮机;不匹配,c#,unit-testing,mocking,moq,C#,Unit Testing,Mocking,Moq,此代码: hub.MockedUserRepository.Setup(r => r.Update(It.IsAny<ControllUser>())) .Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null))) .Verifiable

此代码:

hub.MockedUserRepository.Setup(r => r.Update(It.IsAny<ControllUser>()))
                        .Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null)))
                        .Verifiable();
hub.MockedUserRepository.Setup(r=>r.Update(It.IsAny()))
.Callback((ControlUser usr)=>Console.WriteLine(“NULL=“+(usr.Zombies[0].ConnectionId==NULL)))
.可验证();
将打印

空=真

因此,我认为使用这种匹配将抓住它:

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();
var zombieDisconnectParameterMatcher=It.Is(x=>x.Zombies[0].ConnectionId==null);
hub.MockedUserRepository.Setup(r=>r.Update(zombieDisconnectParameterMatcher))
.Callback((ControlUser usr)=>Console.WriteLine(“NULL=“+(usr.Zombies[0].ConnectionId==NULL)))
.可验证();
但事实并非如此


为什么?

这取决于如何实例化
ControlUser
实例。如果模拟中引用的实例不是测试代码中引用的实际实例,
设置将失败。您需要确保测试代码中引用的
ControllUser
实例与测试代码中的对象是同一个对象。如果不是,则必须使用
it.IsAny()
和回调来测试它,如第一个示例所示。如果没有看到您正在测试的更多代码,很难确定地说。

通过查看,它与表达式树有关。我喜欢这个问题;它们可能相当令人费解。如果您想了解以下方法定义:

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);
}
当使用非空的
ControllUser
执行
Update
方法时(例如,从正在测试的方法),将不会触发回调。它根本不符合标准,因为它不是空的。您也会看到验证失败


要解决此问题,请内联
zombieDisconnectParameterMatcher
变量,或将其设置为表达式类型的变量(例如
expression
)。后者将确保代码不会被执行,而是被视为模拟框架可以推理的表达式(
Update
Zombies[0]调用。ConnectionId==null
?)。

我向您致敬,先生@Caramiriel。
var zombieDisconnectParameterMatcher = It.Is<ControllUser>(x => x.Zombies[0].ConnectionId == null);
hub.MockedUserRepository.Setup(r => r.Update(null))
    .Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null)))
    .Verifiable();