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?
CodePudding user response:
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.
