【发布时间】:2015-10-29 07:55:44
【问题描述】:
我目前正在将 API 重构为异步操作,我需要重构异步测试。我有与Moq documentation类似的情况:
// returning different values on each invocation
var mock = new Mock<IFoo>();
var calls = 0;
mock.Setup(foo => foo.GetCountThing())
.Returns(() => calls)
.Callback(() => calls++);
// returns 0 on first invocation, 1 on the next, and so on
Console.WriteLine(mock.Object.GetCountThing());
我需要将其更改为:
// returning different values on each invocation
var mock = new Mock<IFoo>();
var calls = 0;
mock.Setup(foo => foo.GetCountThingAsync())
.ReturnsAsync(calls)
.Callback(() => calls++);
// returns 0 on first invocation, 1 on the next, and so on
Console.WriteLine(mock.Object.GetCountThingAsync());
但是由于ReturnAsync() 还不支持 lambda,因此调用了回调,但显然是在不同的上下文中,因此该变量仍然是下一次调用的值,而不是增加。有没有办法解决这个问题?
【问题讨论】:
标签: c# asp.net-mvc unit-testing asynchronous moq