gpt4 book ai didi

c# - xUnit 不等待异步测试

转载 作者:可可西里 更新时间:2023-11-01 02:58:44 27 4
gpt4 key购买 nike

在 VS 2013 上,我无法让这个异步测试失败。

我有 xUnit 1.8.0.1539(从 nuget 安装),带有 xUnit Test Runner VS 扩展 (0.99.5)。所有电流,据我所知。

我碰巧在单元测试中也有 Moq、AutoFixture 和 FluentAssertions 引用,但我认为这不重要(但我承认它以防万一)。

我已经在我的解决方案的其他方面进行了异步单元测试,并且它们有效。

我在这个新创建的测试中遗漏了一些东西,我不知道我遗漏了什么或做错了什么。

注意 SUT 代码并不完整。我只是想先让红灯亮起来,然后再编写代码让测试变绿。

测试代码如下:

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();
}
}
}

这是 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());
}
}
}

显然,这个测试应该失败! Null 和 NotNull 的断言不可能都成功,所以我的结论是测试在完成从 SUT 获得响应之前就退出了。

我错过了什么?

或者,有没有更好的方法让我在编写 SUT 代码之前启动异步测试以确保它失败?

最佳答案

You need xUnit 1.9使 async 单元测试正常工作。

关于c# - xUnit 不等待异步测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22945080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com