【发布时间】:2015-04-15 15:16:46
【问题描述】:
针对另一个SO questions,我在使用 Xunit 和 Visual Studio 2015 ctp6 运行异步任务时遇到了一个问题。
代码如下:
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.TestHost;
using Microsoft.Framework.DependencyInjection;
using Xunit;
using Microsoft.AspNet.Builder;
using System.Net.Http;
namespace Multi.Web.Api
{
public class TestServerHelper : IDisposable
{
public TestServerHelper()
{
ClientProvider = new TestClientProvider();
ApiServer = TestServer.Create((app) =>
{
app.UseServices(services =>
{
services.AddTransient<IClientProvider>(s => ClientProvider);
});
app.UseMulti();
});
}
public TestClientProvider ClientProvider { get; private set; }
public TestServer ApiServer { get; private set; }
public void Dispose()
{
ApiServer.Dispose();
ClientProvider.Dispose();
}
}
public class MultiMiddlewareTest : IClassFixture<TestServerHelper>
{
TestServerHelper _testServerHelper;
public MultiMiddlewareTest(TestServerHelper testServerHelper)
{
_testServerHelper = testServerHelper;
}
[Fact]
public async Task ShouldReturnToday()
{
using (HttpClient client = _testServerHelper.ApiServer.CreateClient())
{
var response = await client.GetAsync("http://localhost/today");
String content = await response.Content.ReadAsStringAsync();
Assert.Equal(content, "2015-04-15 count is 1");
}
}
[Fact]
public async Task ShouldReturnYesterday()
{
using (HttpClient client = _testServerHelper.ApiServer.CreateClient())
{
var response = await client.GetAsync("http://localhost/yesterday");
String content = await response.Content.ReadAsStringAsync();
Assert.Equal(content, "2015-04-14 count is 1");
}
}
}
}
在Visual Studio TestExplorer中,在运行测试时(右键单击并选择debug selected test)是可以的,但是运行所有时,没有通过,我有以下错误
Message : Response status code does not indicate success : 404 (Not Fount)
所有代码都可以在另一个问题中找到,在那个问题中,我回答了如何使用多个 TestServer 实例来模拟外部 Api。我认为这与一些同步上下文有关。
我认为我的 Helper 写得不好,因为我看到它在调用实际完成之前处理对象(有时不是......)。有人有同样的问题并对此有解决方案吗?
【问题讨论】:
-
你使用的是 xunit.runner.kre 还是 xunit.runnner.aspnet。 kre runner 中存在问题:github.com/xunit/aspnet.xunit/issues/20。但是使用aspnet runner还有一个问题:VS 2015 CT6 doesn't discover tests
-
我正在使用 xunit.runnner.aspnet。对于未发现的测试,如果您将文件命名为与类相同的名称,则可以。所以在文件 MyTest.cs 中,类名必须是 MyTest。如果您有不同的名称,我看到 VS TestExplorer 无限循环并尝试发现但什么也没发现。
标签: c# asp.net asp.net-core xunit.net