没有调用 Moq 的回调? [英] Callback of Moq is not called?

查看:72
本文介绍了没有调用 Moq 的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中.Callback()Assert.Equal(...) 从不调用?

In the following code. The Assert.Equal(...) of the Callback() is never called?

var test = "Test";

var command = new MyCommand { V = test };

var mock = new Mock<IRepository>(); // IRepository has the method of Save()
var p = new P(test);
mock.Setup(x => x.Save(p))
    .Callback<P>(x => Assert.Equal(x.Value, test)); // break point on Assert.Equal not hit

var sut = new C(mock.Object);
var result = await sut.M(command);

推荐答案

您有:

.Setup(x => x.Save(p))

但是您确定 SUT 中使用的 P 仅等于"那个 p 吗?相反,您可以这样做:

but are you sure the P used in your SUT is "equal to" just that p? Instead you could do:

.Setup(x => x.Save(It.IsAny<P>()))

在那种情况下,设置(和回调)将适用于任何参数.

and in that case the set-up (and call-back) would apply to any argument.

这篇关于没有调用 Moq 的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆