【发布时间】:2014-05-21 14:58:36
【问题描述】:
在 VS 2013 上,我无法让这个异步测试失败。
我有 xUnit 1.8.0.1539(从 nuget 安装),带有 xUnit Test Runner VS 扩展 (0.99.5)。所有当前,AFAIK。
我碰巧在单元测试中也有 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 代码之前启动异步测试以确保它失败吗?
【问题讨论】:
标签: c# unit-testing asynchronous tdd xunit.net