在.NET Core测试项目中读取appsettings json值

编程入门 行业动态 更新时间:2024-10-25 02:22:40
本文介绍了在.NET Core测试项目中读取appsettings json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的Web应用程序需要从appsettings.json文件读取Document DB密钥.我创建了一个具有键名的类,并在ConfigureaServices()中将Config部分读取为:

My Web application needs to read the Document DB keys from appsettings.json file. I have created a class with the key names and reading the Config section in ConfigureaServices() as:

public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); services.AddSession(); Helpers.GetConfigurationSettings(services, Configuration); DIBuilder.AddDependency(services, Configuration); }

我正在寻找在Test项目中读取键值的方法.

I'm looking for the ways to read the Key values in Test project.

推荐答案

这是基于博客帖子 在.NET Core单元测试项目中使用配置文件 (为.NET Core 1.0编写).

This is based on the blog post Using Configuration files in .NET Core Unit Test Projects (written for .NET Core 1.0).

  • 在集成测试项目的根目录中创建(或复制)appsettings.test.json,并在属性中将"Build Action"指定为Content,将"Copy if newnew"复制到Output Directory.请注意,最好使用与常规appsettings.json不同的文件名(例如appsettings.test.json),因为如果使用相同的名称,则主项目中的文件可能会覆盖测试项目中的文件.

  • Create (or copy) the appsettings.test.json in the Integration test project root directory, and in properties specify "Build Action" as Content and "Copy if newer" to Output Directory. Note that it’s better to have file name (e.g. appsettings.test.json ) different from normal appsettings.json, because it is possible that a file from the main project override the file from the test project, if the same name will be used.

    包括JSON配置文件NuGet包(Microsoft.Extensions.Configuration.Json)(如果尚未包含的话).

    Include the JSON Configuration file NuGet package (Microsoft.Extensions.Configuration.Json) if it's not included yet.

    在测试项目中创建一个方法,

    In the test project create a method,

    public static IConfiguration InitConfiguration() { var config = new ConfigurationBuilder() .AddJsonFile("appsettings.test.json") .Build(); return config; }

  • 照常使用配置

  • Use the configuration as usual

    var config = InitConfiguration(); var clientId = config["CLIENT_ID"]

  • 顺便说一句:您还可能很感兴趣,如 与IOptions<>的集成测试在.NET Core中 :

    BTW: You also may be interesting in reading the configuration into the IOptions class as described in Integration test with IOptions<> in .NET Core:

    var options = config.Get<MySettings>();

    更多推荐

    在.NET Core测试项目中读取appsettings json值

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

    发布评论

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

    >www.elefans.com

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