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");
mockConfigRepo
mocking? – Novick