我有一个示例服务器
void Parse()
{
unsined errorLevel = 0;
while(auto p = CreateParser(errorLevel))
{
try
{
p->parse(); /*...*/
//deallocate if ParserPtr is not shared_ptr or other smart ptr
return;
}
catch(Exception & e)
{
++errorLevel;
/*Extra exception handling, deallocating p if needed*/
}
}
//Handle case, when no parser succeeded
}
在启动类中配置:
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
我正在使用xunit测试(学习):
public void Configure(IApplicationBuilder app)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
以后
public TestFixture()
{
var builder = new WebHostBuilder().UseStartup<TStartup>();
_server = new TestServer(builder);
Client = _server.CreateClient();
Client.BaseAddress = new Uri(address);
}
一切都好(200)。现在我正在改变Startup.cs
var response = await Client.GetAsync("http://localhost:51021/");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var responseString = await response.Content.ReadAsStringAsync();
var content = await response.Content.ReadAsStringAsync();
Assert.Contains("Hello World!", content);
如果我使用浏览器启动应用程序,一切正常(显示index.html)。但是如果我用我收到的TestServer调用它(404 Not found)。我的错误在哪里?
答案 0 :(得分:2)
XUnit从其他目录启动该站点。我们必须像这样解决它:https://github.com/aspnet/StaticFiles/blob/d692066b2bd711653150ad2cccc2268583355532/test/Microsoft.AspNetCore.StaticFiles.Tests/StaticFilesTestServer.cs#L20
答案 1 :(得分:0)