使用ASPNetCore和ServiceStack进行集成测试

时间:2017-08-04 15:13:35

标签: asp.net-core servicestack

我有一个使用ServiceStack Core的ASP.Net Core 1.1项目。我正在尝试编写一个基本的集成测试,看起来像这样:

[TestFixture]
public class BasicStartupTest
{
    TestServer Server;
    HttpClient Client;
    const string BASE_URL = "http://localhost:57893";
    [OneTimeSetUp]
    public void OneTimeSetup()
    {
        Server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
        Client = Server.CreateClient();
    }

    [OneTimeTearDown]
    public void OneTimeTearDown()
    {
        Server.Dispose();
    }

    [Test]
    public async Task ShouldHitHealthEndpointAndReturn200()
    {
        var response = await Client.GetAsync("/health");
        response.EnsureSuccessStatusCode();
    }
}

问题是,当它试图从客户端实例运行GetAsync函数时,我得到以下堆栈跟踪:

Result StackTrace:  at ServiceStack.Host.NetCore.NetCoreRequest.get_UserHostAddress() in C:\TeamCity\buildAgent\work\a61371dd01fad6bd\src\ServiceStack\Host\NetCore\NetCoreRequest.cs:line 215
   at ServiceStack.HttpRequestExtensions.GetAttributes(IRequest request) in C:\TeamCity\buildAgent\work\a61371dd01fad6bd\src\ServiceStack\HttpRequestExtensions.cs:line 844
   at ServiceStack.AppHostBase.ProcessRequest(HttpContext context, Func`1 next) in C:\TeamCity\buildAgent\work\a61371dd01fad6bd\src\ServiceStack\AppHostBase.NetCore.cs:line 113
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---

继续,但是当我尝试在集成测试中调用端点时,ServiceStack中的某些get_UserHostAddress函数出现了问题。

如何解决此错误?

1 个答案:

答案 0 :(得分:2)

使用启用了真实自我主机HTTP Server的proper Integration Test,对于.NET Core,ServiceStack.Kestrel NuGet包中提供了AppSelfHostBase类。