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

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

问题描述

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

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

我正在查看的特定部分是 env.ContentRootPath.我做了一些挖掘,看起来我的 appsettings.json 实际上并没有被复制到 bin 文件夹,但这很好,因为 ContentRootPath 是返回 MySolutionsrcMyProject.Api,这是 appsettings.json 文件所在的位置.

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 MySolutionsrcMyProject.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);
    }

这基本上是从指南中复制和粘贴.当我调试这个测试时,ContentRootPath实际上是MySolutionsrcMyProject.IntegrationTestsinDebug et461,这显然是测试项目的构建输出文件夹和再次 appsettings.json 文件不存在(是的,我在测试项目本身中有另一个 appsettings.json 文件)所以测试在创建 TestServer 时失败.

This is basically copy and paste from the guide. When I debug this test, ContentRootPath is actually MySolutionsrcMyProject.IntegrationTestsinDebug et461, 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.

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

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

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

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

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?

推荐答案

最后我按照这个指南,特别是页面底部的集成测试部分.这消除了将 appsettings.json 文件复制到输出目录的需要.相反,它告诉测试项目 Web 应用程序的实际目录.

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.

至于将 appsettings.json 复制到输出目录,我也设法让它工作.结合 dudu 的回答,我使用了 include 而不是 includeFiles 所以结果部分看起来像这样:

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天全站免登陆