简体   繁体   English

xUnit没有等待异步测试

[英]xUnit not awaiting async test

On VS 2013, I can't get this async test to fail. 在VS 2013上,我无法让这个异步测试失败。

I have xUnit 1.8.0.1539 (installed from nuget), with the xUnit Test Runner VS extension (0.99.5). 我有xUnit 1.8.0.1539(从nuget安装),xUnit Test Runner VS扩展(0.99.5)。 All current, AFAIK. 目前所有的AFAIK。

I happen to also have Moq, AutoFixture, and FluentAssertions reference in the unit test, but I don't think that matters (but I'm admitting it in case it does). 我碰巧在单元测试中也有Moq,AutoFixture和FluentAssertions参考,但我认为不重要(但我承认它以防万一)。

I have done async unit tests in other areas of my solution, and they work. 我已经在我的解决方案的其他方面完成了异步单元测试,并且它们可以工作。

I'm missing something with this newly created tests, and I can't tell what I'm missing or doing wrong. 我错过了这些新创建的测试,我不知道我错过了什么或做错了什么。

NOTE The SUT code is not meant to be complete. 注意 SUT代码并不完整。 I'm just trying to get the red light first, before I write the code to make the test go green. 在我编写代码以使测试变为绿色之前,我只是想先获得红灯。

Here's the test code: 这是测试代码:

using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace MobileApp.Proxy.Test
{
public class WhenRetrievingPriceDataFromClient
{
    [Fact]
    public async Task GroupReportIsReturnedWithSomeData()
    {
        // arrange
        var sut = new Client();

        // act
        var actual = await sut.GetReportGroupAsync();

        // assert

        // Xunit test
        Assert.Null(actual);
        Assert.NotNull(actual);
        // FluentAssertions
        actual.Should().BeNull();
        actual.Should().NotBeNull();
    }
}
}

And here is the SUT code: 这是SUT代码:

using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using MobileApp.Proxy.Properties;

namespace MobileApp.Proxy
{
    public class Client
    {
        public async Task<ReportGroup> GetReportGroupAsync()
        {
            return await Task.FromResult(new ReportGroup());
        }
    }
}

Obviously, this test should fail! 显然,这个测试应该失败! The Asserts for Null and NotNull can't both succeed, so my conclusion is that the test is exiting before it finishes getting the response from the SUT. Null和NotNull的Asserts不能同时成功,所以我的结论是测试在完成从SUT获得响应之前就退出了。

What did I miss? 我错过了什么?

OR, is there a better way I should have started an async test to make sure it fails before writing the SUT code? 或者,有没有更好的方法我应该在编写SUT代码之前启动异步测试以确保它失败?

您需要xUnit 1.9才能使async单元测试正常工作。

Async tests are supported in xUnit v1.9 or later. xUnit v1.9或更高版本支持异步测试。 If you're stuck with an earlier version, you'll need to do something like this: 如果您遇到早期版本,则需要执行以下操作:

[Fact]
public void GroupReportIsReturnedWithSomeData()
{
     GroupReportIsReturnedWithSomeDataAsync().Wait();
}

private async Task GroupReportIsReturnedWithSomeDataAsync()
{
    // arrange
    var sut = new Client();

    // act
    var actual = await sut.GetReportGroupAsync();

    // assert

    // Xunit test
    Assert.Null(actual);
    Assert.NotNull(actual);
    // FluentAssertions
    actual.Should().BeNull();
    actual.Should().NotBeNull();
}

Basically, the test method blocks until the async test method completes, whether it'd due to successful completion or fault (eg, a failed assertion). 基本上,测试方法会阻塞,直到异步测试方法完成,无论是由于成功完成还是故障(例如,断言失败)。 In the case of a fault, the exceptions will propagate to the main test thread through Wait() . 在出现故障的情况下,异常将通过Wait()传播到主测试线程。

You may want to pass a timeout to Wait() so your test will fail if it hasn't completed after a certain amount of time. 您可能希望将超时传递给Wait()这样如果在一定时间后没有完成测试,则测试将失败。 As written, the test could block indefinitely if the async method never completes. 如上所述,如果异步方法永远不会完成,则测试可能会无限期地阻塞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM