AppSettings.json,用于ASP.NET Core中的集成测试 [英] AppSettings.json for Integration Test in 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 The particular part I'm looking at is the So in my integration test project I have this test: This is basically copy and paste from the guide. When I debug this test, I tried getting around this by modifying the test I hoped this would copy the 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 As for copying the 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屋!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
. 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.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
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
.project.json
file."buildOptions": {
"emitEntryPoint": true,
"copyToOutput": {
"includeFiles": [
"appsettings.json"
]
}
}
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.appsettings.json
file to the output directory. Instead it tells the test project the actual directory of the web application.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"
}
}