为什么TestServer(AspNetCore)在静态文件上出现404错误?

时间:2016-06-14 14:45:09

标签: c# asp.net-core .net-core-rc2

我有一个示例服务器

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)。我的错误在哪里?

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

另一种方法是将这些文件复制到您的单元测试项目中

enter image description here

然后

enter image description here

只要您维护要测试的项目的相同结构文件夹,它将找到文件并将其传递给依赖项注入过程。