我的Web应用程序需要从appsettings.json文件中读取Document DB键.我创建了一个带有键名的类,并在以下位置读取Config部分ConfigureaServices():
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);
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找在Test项目中读取Key值的方法.
我创建了一个asp.net核心Web应用程序和一个单元测试应用程序.
我使用"ASP.NET Core Web Application(.NET Core)"模板创建了asp.net应用程序,并使用"类库(.NET Core)"模板创建了单元测试项目.
我使用以下文章中的说明配置了MSTest:
宣布MSTest Framework支持.NET Core RC2/ASP.NET Core RC2
我已将应用程序组织到控制器和服务中,控制器从appsettings.json读取值并将这些值作为参数传递给服务方法.
我有一个AppSettings类如下
public class AppSettings
{
public string Setting1 { get; set; }
public string Setting2 { get; set; }
public string Setting3etc { get; set; }
}
public static class App
{
public static AppSettings Settings { get; set; }
public static void ConfigureSettings(IOptions<AppSettings> settings)
{
Settings = settings.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
控制器构造函数如下
public ValuesController(IOptions<AppSettings> settings)
{
App.ConfigureSettings(settings);
}
Run Code Online (Sandbox Code Playgroud)
在Startup.cs中,我在ConfigureServices方法中有以下行
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
Run Code Online (Sandbox Code Playgroud)
我从以下文章中了解到了这种技术
我在手动构造一个时遇到了麻烦IServiceProvider,它将允许我的单元测试使用它来引入共享的测试配置GetService<IOptions<MyOptions>>
我创建了一些单元测试来说明我的问题,如果可以用于回答问题,也可以在此处找到该代码的存储库。
JSON
{
"Test": {
"ItemOne": "yes"
}
}
Run Code Online (Sandbox Code Playgroud)
期权类别
public class TestOptions
{
public string ItemOne { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
测试
这些测试都失败了,ConfigureWithBindMethod并且ConfigureWithBindMethod都SectionIsAvailable合格了。因此,据我所知,该部分正按预期从JSON文件中使用。
[TestClass]
public class UnitTest1
{
[TestMethod]
public void ConfigureWithoutBindMethod()
{
var collection = new ServiceCollection();
var config = new ConfigurationBuilder()
.AddJsonFile("test.json", optional: false)
.Build();
collection.Configure<TestOptions>(config.GetSection("Test"));
var services = collection.BuildServiceProvider();
var options = services.GetService<IOptions<TestOptions>>();
Assert.IsNotNull(options);
}
[TestMethod]
public void ConfigureWithBindMethod()
{
var collection = new …Run Code Online (Sandbox Code Playgroud) 在 ASP.NET Core 2 中,我有以下类 IOptions<T>
public class MyOptions
{
public string Option1 { get; set; }
public string Option2 { get; set; }
}
public class MyService:IMyService
{
private readonly MyOptions _options;
public MyService(IOptions<MyOptions> options)
{
_options = options.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"MyOptions": {
"Option1": "option 1 value",
"Option2": "option 2 value"
}
}
Run Code Online (Sandbox Code Playgroud)
然后我 startup.cs 我注册选项如下
services.Configure<MyOptions>(Configuration.GetSection("MyOptions"));
Run Code Online (Sandbox Code Playgroud)
dotnet 能够注入IOptions<MyOptions>MyService 类。
现在,我有集成测试项目。我已将相同的 appsettings.json 复制到集成测试项目中。我想创建IOptions<MyOptions>从 …
asp.net-core ×3
.net-core ×2
appsettings ×2
.net ×1
c# ×1
coreclr ×1
mstest ×1
unit-testing ×1