AppSettings.json,用于ASP.NET Core中的集成测试 [英] AppSettings.json for Integration Test in ASP.NET Core

查看:92
本文介绍了AppSettings.json,用于ASP.NET Core中的集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循此指南.我在使用appsettings.json配置文件的API项目中有一个Startup.

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)                
            .AddEnvironmentVariables();
        Configuration = builder.Build();

        Log.Logger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .ReadFrom.Configuration(Configuration)
            .CreateLogger();
    }

我正在查看的特定部分是env.ContentRootPath.我进行了一些挖掘,看来我的appsettings.json实际上并未复制到bin文件夹中,但这很好,因为ContentRootPath返回了MySolution\src\MyProject.Api\,这是appsettings.json文件所在的位置./p>

因此,在我的集成测试项目中,我进行了以下测试:

public class TestShould
{
    private readonly TestServer _server;
    private readonly HttpClient _client;

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

    [Fact]
    public async Task ReturnSuccessful()
    {
        var response = await _client.GetAsync("/monitoring/test");
        response.EnsureSuccessStatusCode();

        var responseString = await response.Content.ReadAsStringAsync();

        Assert.Equal("Successful", responseString);
    }

这基本上是指南中的复制和粘贴.当我调试该测试时,ContentRootPath实际上是MySolution\src\MyProject.IntegrationTests\bin\Debug\net461\,这显然是测试项目的构建输出文件夹,并且再次没有appsettings.json文件(是的,我确实在测试项目中还有另一个appsettings.json文件)本身),因此测试无法创建TestServer.

我尝试通过修改测试project.json文件来解决此问题.

"buildOptions": {
    "emitEntryPoint": true,
    "copyToOutput": {
        "includeFiles": [
            "appsettings.json"
       ]
    }
}

我希望这会将appsettings.json文件复制到构建输出目录,但它抱怨项目缺少用于入口点的Main方法,将测试项目视为控制台项目.

该如何解决?我在做错什么吗?

解决方案

最后,我遵循了

我不完全确定为什么会这样,但确实如此.我快速浏览了一下文档,但是找不到任何真正的区别,并且由于从根本上解决了原始问题,所以我没有进一步查找.

I am following this guide. I have a Startup in the API project that uses an appsettings.json configuration file.

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)                
            .AddEnvironmentVariables();
        Configuration = builder.Build();

        Log.Logger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .ReadFrom.Configuration(Configuration)
            .CreateLogger();
    }

The particular part I'm looking at is the env.ContentRootPath. I did some digging around and it looks like my appsettings.json isn't actually copied to the bin folder but that is fine as ContentRootPath is returning MySolution\src\MyProject.Api\, which is where the appsettings.json file is located.

So in my integration test project I have this test:

public class TestShould
{
    private readonly TestServer _server;
    private readonly HttpClient _client;

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

    [Fact]
    public async Task ReturnSuccessful()
    {
        var response = await _client.GetAsync("/monitoring/test");
        response.EnsureSuccessStatusCode();

        var responseString = await response.Content.ReadAsStringAsync();

        Assert.Equal("Successful", responseString);
    }

This is basically copy and paste from the guide. When I debug this test, ContentRootPath is actually MySolution\src\MyProject.IntegrationTests\bin\Debug\net461\, which is obviously the build output folder for the test project and again the appsettings.json file is not there (yes I do have another appsettings.json file in the test project itself) so the test fails at creating the TestServer.

I tried getting around this by modifying the test project.json file.

"buildOptions": {
    "emitEntryPoint": true,
    "copyToOutput": {
        "includeFiles": [
            "appsettings.json"
       ]
    }
}

I hoped this would copy the appsettings.json file to the build output directory but it complains about the project missing a Main method for an entry point, treating the test project like a console project.

What can I do to get around this? Am I doing something wrong?

解决方案

In the end, I followed this guide, specifically the Integration Testing section towards the bottom of the page. This removes the need to copy the appsettings.json file to the output directory. Instead it tells the test project the actual directory of the web application.

As for copying the appsettings.json to the output directory, I also managed to get that to work. Combined with the answer from dudu, I used include instead of includeFiles so the resulting section would look something like this:

"buildOptions": {
    "copyToOutput": {
        "include": "appsettings.json"
    }
}

I'm not entirely sure why this works but it does. I had a quick look at the documentation but couldn't find any real differences and since the original problem was essentially solved I didn't look further.

这篇关于AppSettings.json,用于ASP.NET Core中的集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆