AppSettings.json,用于ASP.NET Core中的集成测试

编程入门 行业动态 更新时间:2024-10-24 22:28:00
本文介绍了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方法,将测试项目视为控制台项目.

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

解决方案

最后,我遵循了指南,尤其是页面底部的集成测试部分.这样就无需将appsettings.json文件复制到输出目录.相反,它告诉测试项目Web应用程序的实际目录.

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

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

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

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中的集成测试

本文发布于:2023-11-02 15:02:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   json   AppSettings   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!