3

I have the following appsettings.json configuration.

"SettingsConfig": [
{
  "Name": "Something",
  "Node": "Something",
  "SettingName": "Something"
},
{
  "Name": "Something",
  "Node": "Something",
  "SettingName": "Something"
}]

I want to write UnitTest ,but the following syntax does not work.

_configuration = A.Fake<IConfiguration>();
A.CallTo(() => _configuration.GetSection("SettingsConfig")).Returns(new List<SettingsConfig>());

Error message: IConfigurationSection does not contain definition for Returns.

How IConfiguration can be mocked with FakeItEasy syntax in order to apply mock data for UnitTesting?

Sarah
  • 65
  • 4
  • 1
    Are you sure you're not missing closing parenthesis `)` before `.Returns`? I'd expect that error if you had code similar to: `A.CallTo(() => _configuration.GetSection("SettingsConfig").Returns(new List());` – haldo Jan 11 '22 at 11:23
  • 1
    Nope, the brackets are fine. You can copy-paste the code and see that they are proper. – Sarah Jan 11 '22 at 11:29
  • Is this the exact error message? I get a different message: `'IReturnValueArgumentValidationConfiguration' does not contain a definition for 'Returns' and the best extension method overload 'ReturnValueConfigurationExtensions.Returns>(IReturnValueConfiguration>>, List)' requires a receiver of type 'IReturnValueConfiguration>>'` – Thomas Levesque Jan 12 '22 at 13:08
  • I think the problem is that `GetSection` returns a `IConfigurationSection`, not a `List`. So you can't configure the method to return `List`, it *has* to return a `IConfigurationSection`. – Thomas Levesque Jan 12 '22 at 14:17
  • 1
    But I need the List() to execute the business logic after that or it will stay null – Sarah Jan 13 '22 at 11:51

2 Answers2

2

If you take a look at the return value of IConfiguration.GetSection(), you'll see that it expects to return an IConfigurationSection. That's the first issue here.

Beyond that, in order to pull out your List from the config, you would need to do something like

_configuration.GetSection("SettingsConfig").Get<List<SettingsConfig>>();

Unfortunately, the .Get<> method on an IConfigurationSection is an extension method, which cannot be faked by FakeItEasy. You will get the following error:

The current proxy generator can not intercept the method Microsoft.Extensions.Configuration.ConfigurationBinder.Get... - Extension methods can not be intercepted since they're static.

You'll need to put another interface in front of the IConfiguration that wraps the extension method if you need to test it.

Otherwise to fake your configuration file, you could do something like the following:

var fakeConfigSection = A.Fake<IConfigurationSection>();
A.CallTo(() => fakeConfigSection["Name"])
    .Returns("MyFakedConfigValue");

var fakeConfig = A.Fake<IConfiguration>();
A.CallTo(() => fakeConfig.GetSection("SettingsConfig"))
    .Returns(fakeConfigSection);
Isaac
  • 96
  • 1
  • 5
-2

First, you should implement method that reads from file, which exists in the projects of unit test. So, if there is no file .json that you can read from you won;t be able to GetSection at all. So add file there then apply:

private IConfiguration ApplyConfiguration()
    {
        var configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.AddJsonFile("settingsConfig.json");
        var config = configurationBuilder.Build();

        config.GetSection("settingsConfig").Get<List<SettingsConfig>>();

        return config;
    }

Then in your business logic add:

var config = ApplyConfiguration();

You will see that config contains all the configuration there, so no need to call OnCall .Returns() at all.

Radost
  • 131
  • 1
  • 11