正如@Tratcher 作为评论回复的那样:xUnit 支持异步,所以让你的测试异步并返回任务(否则 xUnit 不知道你的测试结果,它甚至会在它完成运行之前通过):
[Fact]
public async Task Get_hello_should_ignore_bob()
{
// Given a sample client
var client = SampleClient();
// When I call POST /account/signin
var value = new StringContent("");
var response = await client.PostAsync("/account/signin", value);
// Then the result should be Hello
string result = await response.Content.ReadAsStringAsync();
result.Should().Be("\"Hello\"");
}
现在您的测试是async,您可以使用await,而不是在任务上调用 .Result (这是导致您的问题的原因):
此外,值得注意的是为什么会发生这种情况。查看更多关于死锁和 SynchronizationContext