用于集成测试的ASPNet.Core HostingEnvironment?

时间:2017-10-27 15:27:06

标签: c# unit-testing asp.net-core asp.net-core-mvc-2.0

为我的ASPNet.Core网络应用程序https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing构建集成测试, 我遇到了一个问题。当我运行应用程序并且读取配置并包含apsettings.json文件中的所有信息时,将运行Startup。 现在,当我运行集成测试时,如下所示。运行启动但配置不同。为什么会发生这种情况,我如何确保它读取 应用程序本身的那个?

[TestFixture]
class HSMControllerTests
{
    private readonly TestServer _server;
    private readonly HttpClient _client;

    public HSMControllerTests()
    {
        _server = new TestServer(new WebHostBuilder()
        .UseStartup<Startup>());
        _client = _server.CreateClient();
    }

    [Test]
    public async global::System.Threading.Tasks.Task GET_PingHSM_ShouldSucceedAsync()
    {
        HttpResponseMessage response = await _client.GetAsync("HSM/PingHSM");
        Assert.NotNull(response);
        Assert.IsInstanceOf<OkObjectResult>(response);
    }
}

我收到例外Missing configuration section ServiceConfig.

这就是我的应用程序Program.cs中构建WebHost的方式:

WebHost.CreateDefaultBuilder(args)
       .UseStartup<Startup>()
       .UseKestrel(o => o.AddServerHeader = false)
       .Build();

在测试代码中如何构建它的区别可能是问题吗?

修改后的ControllerTest构造函数:

public HSMControllerTests()
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(Path.GetFullPath(@"../../../../HSM.WebApi.IntegrationTests"))
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddEnvironmentVariables();

    Configuration = builder.Build();

    _server = new TestServer(WebHost.CreateDefaultBuilder()
        .UseStartup<Startup>());
    _client = _server.CreateClient();
}

现在,如何将新配置注入Startup?我们的Startup定义如下:

public Startup(IConfiguration configuration, IHostingEnvironment environment)
        : base(typeof(Startup), configuration, environment)
{
}

根据@poke

的上一篇文章进行修改
_server = new TestServer(WebHost.CreateDefaultBuilder()
    .ConfigureAppConfiguration(configBuilder => new ConfigurationBuilder()
        .SetBasePath(Path.GetFullPath(@"../../../../HSM.WebApi.IntegrationTests"))
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddEnvironmentVariables()
    )
    .UseStartup<Startup>());

有点工作,有点......

var config = new ConfigurationBuilder()
    .SetBasePath(Path.GetFullPath(@"../../../../HSM.WebApi.IntegrationTests"))
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables();

var Configuration = config.Build();

_server = new TestServer(WebHost.CreateDefaultBuilder()
    .UseConfiguration(Configuration)
    .UseStartup<Startup>());
_client = _server.CreateClient();

但是现在,HostingEnvironment被设置为测试应用程序的目录与Web应用程序的目录,它尝试从HomeController构造函数中读取appsettings.json文件,这里:

public HSMController(IHostingEnvironment hostingEnvironment)
{
    _hostingEnvironment = hostingEnvironment;
    contentRootPath = _hostingEnvironment.ContentRootPath;
}

2 个答案:

答案 0 :(得分:5)

appsettings.json文件复制到集成测试项目文件夹并将其设置为“始终复制”并且它可以正常工作。

答案 1 :(得分:4)

ASP .Net Core 2.0

将appsettings.json文件复制到集成测试中,并将它们设置为始终复制。

创建MyTestServer类,因为它可能会在许多测试中重用。请记住,CreateDefaultBuilder会自动加载您的appsettings并使用环境。

  public class ApiTestServer : TestServer
  {
    public ApiTestServer() : base(WebHostBuilder())
    { }

    private static IWebHostBuilder WebHostBuilder() =>
      WebHost.CreateDefaultBuilder()
        .UseStartup<Startup>()
        .UseEnvironment("Local")
        .UseKestrel(options =>
        {
          options.Listen(IPAddress.Any, 5001);
        });
  }

然后在你的测试中

public SettingsTests()
{
  TestServer = new ApiTestServer();
  HttpClient = TestServer.CreateClient();
}