I have a list like following in config.json file `
{
\"foo\": {
\"bar\": [
\"1\",
\"2\",
\"3\"
]
}
}`
I am ab
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();
oneSectionMock.Setup(s => s.Value).Returns("1");
var twoSectionMock = new Mock();
twoSectionMock.Setup(s => s.Value).Returns("2");
var fooBarSectionMock = new Mock();
fooBarSectionMock.Setup(s => s.GetChildren()).Returns(new List { 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