问题
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