我有一个xUnit(2.1.0)测试始终会挂起/死锁。以下是代码,其中类/方法的名称已更改以提高清晰度和保密性:
[Fact]
public void DocumentModificationTest()
{
Exception ex = null;
using (TestDependency testDependency = new TestDependency())
{
TestDependencyDocument testDependencyDoc = testDependency.Document;
MockTestDependency fakeDependency = new MockTestDependency();
try
{
DoStuffToTheDocument(testDependencyDoc, "fileName.doc", fakeDependency);
}
catch (Exception e)
{
ex = e;
}
}
Assert.Null(ex);
}
如果我设置了一个断点,并一步一步地执行,直到 assert 语句,我可以看到 ex 是 null,测试应该通过并完成,但它却停住了,我从来没有在运行器上看到“测试成功”的提示。
下面是 DoStuffToTheDocument 的代码:
public static void DoStuffToTheDocument(TestDependencyDocument document, string pFileName, MockTestDependency pContainer)
{
pContainer.CheckInTheDocFirst(async () =>
{
//check some stuff
//test returns early here
//check other stuff(test never gets here)
//await method (thus the async anonymous method)
});
}
最后,这就是CheckInTheDocFirst的样子:
public void CheckInTheDocFirst(Action pConfirmAction)
{
pConfirmAction(); //since this is a method in a mock class only used for testing we just call the action
}
这里发生了什么事情?是我的异步等待范式有问题导致测试挂起吗?
async void
方法,这种方法非常难以测试,但不应该导致死锁。你是否存在任何阻塞?(请提供一个最简但完整的例子)。 - Stephen Cleary