36

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);
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Engineer
  • 459
  • 1
  • 4
  • 8
  • 1
    You cannot mock it unless you abstract it out into an interface or a virtual method. What is `mockConfigRepo` mocking? – CodingYoshi May 06 '18 at 16:10
  • 1
    It's mockConfigRepo a Mock object of IConfigurationRoot? And reportLanguages it's a list of strings? Could you specify the error? – Alpha75 May 06 '18 at 16:42
  • 1
    The method IConfigurationSection.Get is an extension method. You cannot mock extension methods. – Alpha75 May 06 '18 at 17:28

6 Answers6

40

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();
danfolkes
  • 404
  • 4
  • 12
Ahamed Ishak
  • 972
  • 11
  • 16
35

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

Pasha Banks
  • 451
  • 6
  • 6
16

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");
  • with that mock i am unable to cast to type... `mockIConfigurationSection.Object.Get();` is null even when mockIConfigurationSection.Object contains json of the instance of SomeClass – Sasha Bond May 26 '22 at 18:50
  • @SashaBond I have the same issue and the problem here is that Get<..> is an extension method (or, in other words, a static method) and Moq does not provide support to "override" a static method. For this, I've choosen to read a configuration from memory stream as reported in this answer https://stackoverflow.com/a/62512210/2621924 – Samuele Furnari Jun 27 '22 at 12:50
12

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.

Nuh Metin Güler
  • 1,075
  • 8
  • 9
  • 3
    It is an alternative way of mocking, but it surely works, and is easier as well. – Gerard Jul 08 '19 at 09:18
  • I think it is not a mocking because you are depending on an external asset. If this asset does not exist the test will give a different result, that is bad. – Renato Ramos Nascimento Jan 28 '20 at 13:52
  • @RenatoRamosNascimento Unit test are just a bunch of setup to test a specific code unit. Ideally yes, there should be a way to set it up to not depend on an external file (See Ahamed Ishak's answer and related comments below), but having to make sure you have the external file prepared for the test is not much different than making sure you have all your mocks configured imho. – Austin_G Oct 31 '20 at 17:29
  • Your unit tests should not depends on external sources like files or database. It breaks the main advantage of unit tests. – 1_bug Jun 08 '21 at 10:35
7

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();
0

For those using FakeItEasy, you can simply do:

var fakeConfiguration = A.Fake<IConfiguration>();
var fakeConfigurationSection = A.Fake<IConfigurationSection>();

fakeConfigurationSection["bar"] = "Your desired value here";

A.CallTo(() => fakeConfiguration.GetSection("foo"))
                .Returns(fakeConfigurationSection);

This might be helpful for getting ConnectionStrings, just replace foo by ConnectionStrings and bar by your connection string key