I have a list like following in config.json file `
{
"foo": {
"bar": [
"1",
"2",
"3"
]
}
}`
I am able to get the list at run-time using
Configuration.GetSection("foo:bar").Get<List<string>>()
I want to mock the configuration.GetSection
to write unit test.
Following syntax is failing
mockConfigRepo
.SetupGet(x => x.GetSection("reportLanguageSettings:reportLanguageList").Get<List<string>>())
.Returns(reportLanguages);
I've encountered same issue and found that I needed to create a mock IConfigurationSection for every element in the array, as well as the array node itself, and then setup the parent node to return children, and children to return their values. In OP example, it would look like this:
var oneSectionMock = new Mock<IConfigurationSection>();
oneSectionMock.Setup(s => s.Value).Returns("1");
var twoSectionMock = new Mock<IConfigurationSection>();
twoSectionMock.Setup(s => s.Value).Returns("2");
var fooBarSectionMock = new Mock<IConfigurationSection>();
fooBarSectionMock.Setup(s => s.GetChildren()).Returns(new List<IConfigurationSection> { oneSectionMock.Object, twoSectionMock.Object });
_configurationMock.Setup(c => c.GetSection("foo:bar")).Returns(fooBarSectionMock.Object);
P.S. I'm using Moq, so please translate to your mock library of choice.
P.P.S. If you are interested in why this ends up working, what unmockable Get() method does, or have a more complex scenario than OP, reading this class may be helpful: https://github.com/aspnet/Extensions/blob/release/2.1/src/Configuration/Config.Binder/src/ConfigurationBinder.cs
I was able to solve it using ConfigurationBuilder. Hope this will help
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();
You can try alternative ways. For example, you can try to create an instance of ConfigurationBuilder in your test class:
string projectPath = AppDomain.CurrentDomain.BaseDirectory.Split(new String[] { @"bin\" }, StringSplitOptions.None)[0];
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(projectPath)
.AddJsonFile("config.json")
.Build();
Note: Please don't forget to add your config.json file to your test project too.
In general, if you have a key/value at the root level and you want to mock this piece of code:
var threshold = _configuration.GetSection("RootLevelValue").Value;
you can do:
var mockIConfigurationSection = new Mock<IConfigurationSection>();
mockIConfigurationSection.Setup(x => x.Key).Returns("RootLevelValue");
mockIConfigurationSection.Setup(x => x.Value).Returns("0.15");
_mockIConfiguration.Setup(x => x.GetSection("RootLevelValue")).Returns(mockIConfigurationSection.Object);
If the key/value is not at the root level, and you want to mock a code that is like this one:
var threshold = _configuration.GetSection("RootLevelValue:SecondLevel").Value;
you have to mock Path
as well:
var mockIConfigurationSection = new Mock<IConfigurationSection>();
mockIConfigurationSection.Setup(x => x.Path).Returns("RootLevelValue");
mockIConfigurationSection.Setup(x => x.Key).Returns("SecondLevel");
mockIConfigurationSection.Setup(x => x.Value).Returns("0.15");
and so on for the third level:
var threshold = _configuration.GetSection("RootLevelValue:SecondLevel:ThirdLevel").Value;
you have to mock Path
as well:
var mockIConfigurationSection = new Mock<IConfigurationSection>();
mockIConfigurationSection.Setup(x => x.Path).Returns("RootLevelValue:SecondLevel");
mockIConfigurationSection.Setup(x => x.Key).Returns("ThirdLevel");
mockIConfigurationSection.Setup(x => x.Value).Returns("0.15");
Just to add on Ahamed Ishak answer. Convert actual objects to JSON would clean up the code and types are respected. Avoid string typo errors, etc.
var appSettings = JsonConvert.SerializeObject(new
{
Security = new SecurityOptions {Salt = "test"}
});
var builder = new ConfigurationBuilder();
builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(appSettings)));
var configuration = builder.Build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With