Callback of Moq is not called?

做~自己de王妃 提交于 2021-02-11 13:41:02

问题


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);

回答1:


You have:

.Setup(x => x.Save(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.



来源:https://stackoverflow.com/questions/59418160/callback-of-moq-is-not-called

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!