我正在尝试发现如何将 async 和 await 关键字应用于我的 xUnit 测试。我正在使用 xUnit 1.9 和 Async CTP 1.3。这是我的测试用例
我有一个接口,它指定一个异步方法调用
public interface IDoStuffAsync
{
Task AnAsyncMethod(string value);
}
我有一个使用接口并调用异步方法的类
public class UseAnAsyncThing
{
private readonly IDoStuffAsync _doStuffAsync;
public UseAnAsyncThing(IDoStuffAsync doStuffAsync)
{
_doStuffAsync = doStuffAsync;
}
public async Task DoThatAsyncOperation(string theValue)
{
await _doStuffAsync.AnAsyncMethod(theValue);
}
}
在我的测试中,我希望检查该方法DoThatAsyncOperation
是否使用正确的值调用该方法,因此我模拟接口并使用 Moq 来验证调用
[Fact]
public async void The_test_will_pass_even_though_it_should_fail()
{
var mock = new Mock<IDoStuffAsync>();
var sut = new UseAnAsyncThing(mock.Object);
mock.Setup(x => x.AnAsyncMethod(It.IsAny<string>()));
await sut.DoThatAsyncOperation("test");
// This won't throw a Moq.MockExcpetion so the test appears to pass
// However it does not run
mock.Verify(x => x.AnAsyncMethod("fail"));
}
此测试使用async
andawait
关键字。当它运行时,它会错误地通过,因为 Moq 应该断言验证失败。调用后的任何代码sut.DoThatAsyncOperation("test");
都不会运行
[Fact]
public void This_will_work_and_assert_the_reslt()
{
var mock = new Mock<IDoStuffAsync>();
var sut = new UseAnAsyncThing(mock.Object);
mock.Setup(x => x.AnAsyncMethod(It.IsAny<string>()));
sut.DoThatAsyncOperation("test").ContinueWith(y => { });
// This won't throw a Moq.MockExcpetion so the test appears to pass
// However it does not run
mock.Verify(x => x.AnAsyncMethod("fail"));
}
这个测试是在没有 await 和 async 关键字的情况下设置的,并且可以通过。
这是 xUnit 和 Moq 的预期行为吗?
更新
感谢斯蒂芬的评论,我设法通过进行两项更改来修复第一个测试。测试现在返回一个 Task 而不是 void,Mock 也返回一个 Task。
[Fact]
public async Task The_test_will_pass_even_though_it_should_fail()
{
var mock = new Mock<IDoStuffAsync>();
var sut = new UseAnAsyncThing(mock.Object);
mock.Setup(x => x.AnAsyncMethod(It.IsAny<string>())).ReturnAsync(true);
await sut.DoThatAsyncOperation("test");
// This now fails as it should
mock.Verify(x => x.AnAsyncMethod("fail"));
}