我在 config.json 文件中有如下列表 `
{
"foo": {
"bar": [
"1",
"2",
"3"
]
}
}`
我可以在运行时使用
获取列表Configuration.GetSection("foo:bar").Get<List<string>>()
我想模拟 configuration.GetSection
来编写单元测试。
以下语法失败
mockConfigRepo
.SetupGet(x => x.GetSection("reportLanguageSettings:reportLanguageList").Get<List<string>>())
.Returns(reportLanguages);
最佳答案
我能够使用 ConfigurationBuilder 解决它。希望这会有所帮助
var appSettings = @"{""AppSettings"":{
""Key1"" : ""Value1"",
""Key2"" : ""Value2"",
""Key3"" : ""Value3""
}}";
var builder = new ConfigurationBuilder();
builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(appSettings)));
var configuration= builder.Build();
关于C# 如何模拟 Configuration.GetSection ("foo:bar").Get<List<string>>(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50201588/