ASPNet.Core 集成测试的托管环境?

ASPNet.Core HostingEnvironment for Integration Tests?

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

[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 定义如下:

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

根据 @poke

最后 post 的修改
_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;
}

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

ASP.Net 核心 2.0

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

创建 MyTestServer Class 因为它可能会在许多测试中重复使用。请记住,CreateDefaultBuilder 将自动加载您的应用程序设置和使用环境。

  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();
}