如何模拟 IConfiguration

我试图模仿一个顶级(不是任何部分的一部分)配置值(。NET 核心的 IConfiguration)。例如,这两个都不起作用(使用 NSubute,但是对于 Moq 或者我相信的任何模拟软件包都是一样的) :

var config = Substitute.For<IConfiguration>();
config.GetValue<string>(Arg.Any<string>()).Returns("TopLevelValue");
config.GetValue<string>("TopLevelKey").Should().Be("TopLevelValue"); // nope
// non generic overload
config.GetValue(typeof(string), Arg.Any<string>()).Returns("TopLevelValue");
config.GetValue(typeof(string), "TopLevelKey").Should().Be("TopLevelValue"); // nope

在我的例子中,我还需要从这个配置实例调用 GetSection

53544 次浏览

IConfiguration.GetSection<T> must be mocked indirectly. I don't fully understand why because NSubstitute, if I understand correctly, creates its own implementation of an interface you're mocking on the fly (in memory assembly). But this seems to be the only way it can be done. Including a top-level section along with a regular section.

var config = Substitute.For<IConfiguration>();
var configSection = Substitute.For<IConfigurationSection>();
var configSubSection = Substitute.For<IConfigurationSection>();
configSubSection.Key.Returns("SubsectionKey");
configSubSection.Value.Returns("SubsectionValue");
configSection.GetSection(Arg.Is("SubsectionKey")).Returns(configSubSection);
config.GetSection(Arg.Is("TopLevelSectionName")).Returns(configSection);


var topLevelSection = Substitute.For<IConfigurationSection>();
topLevelSection.Value.Returns("TopLevelValue");
topLevelSection.Key.Returns("TopLevelKey");
config.GetSection(Arg.Is<string>(key => key != "TopLevelSectionName")).Returns(topLevelSection);


// GetValue mocked indirectly.
config.GetValue<string>("TopLevelKey").Should().Be("TopLevelValue");
config.GetSection("TopLevelSectionName").GetSection("SubsectionKey").Value.Should().Be("SubsectionValue");

You can use an actual Configuration instance with in-memory data.

//Arrange
var inMemorySettings = new Dictionary<string, string> {
{"TopLevelKey", "TopLevelValue"},
{"SectionName:SomeKey", "SectionValue"},
//...populate as needed for the test
};


IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();




//...

Now it is a matter of using the configuration as desired to exercise the test

//...


string value = configuration.GetValue<string>("TopLevelKey");


string sectionValue = configuration.GetSection("SectionName").GetValue<string>("SomeKey");


//...

Reference: Memory Configuration Provider

I do not have idea about NSubstitute, but this is how we can do in Moq. Aproach is same in either cases.

GetValue<T>() internally makes use of GetSection().

You can Mock GetSection and return your Own IConfigurationSection.

This includes two steps.

1). Create a mock for IConfigurationSection (mockSection) & Setup .Value Property to return your desired config value.

2). Mock .GetSection on Mock< IConfiguration >, and return the above mockSection.Object

Mock<IConfigurationSection> mockSection = new Mock<IConfigurationSection>();
mockSection.Setup(x=>x.Value).Returns("ConfigValue");


Mock<IConfiguration> mockConfig = new Mock<IConfiguration>();
mockConfig.Setup(x=>x.GetSection(It.Is<string>(k=>k=="ConfigKey"))).Returns(mockSection.Object);

Mock IConfiguration

Mock<IConfiguration> config = new Mock<IConfiguration>();

SetupGet

config.SetupGet(x => x[It.Is<string>(s => s == "DeviceTelemetryContainer")]).Returns("testConatiner");
config.SetupGet(x => x[It.Is<string>(s => s == "ValidPowerStatus")]).Returns("On");

I could imagine in some rare scenarios it is needed but, in my humble opinion, most of the time, mocking IConfiguration highlight a code design flaw.

You should rely as much as possible to the option pattern to provide a strongly typed access to a part of your configuration. Also it will ease testing and make your code fail during startup if your application is misconfigured (instead than at runtime when the code reading IConfiguration is executed).

If you really(really) need to mock it then I would advice to not mock it but fake it with an in-memory configuration as explained in @Nkosi's answer

While Nkosi's answer works great for simple structures, sometimes you want to be able to have more complex objects (like arrays) without repeating the whole section path and to be able to use the expected types themselves. If you don't really care too much about performance (and should you in your unit tests?) then this extension method might be helpful.

public static void AddObject(this IConfigurationBuilder cb, object model) {
cb.AddJsonStream(new MemoryStream(Encoding.UTF8.GetString(JsonConvert.SerializeObject(model))));
}

And then use it like this

IConfiguration configuration = new ConfigurationBuilder()
.AddObject(new {
SectionName = myObject
})
.Build();

Use SetupGet method to mock the Configuration value and return any string.

var configuration = new Mock<IConfiguration>();
configuration.SetupGet(x => x[It.IsAny<string>()]).Returns("the string you want to return");

We need to mock IConfiguration.GetSection wichs is executed within GetValue extension method.

You inject the IConfiguration:

private readonly Mock<IConfiguration> _configuration;

and the in the //Arrange Section:

_configuration.Setup(c => c.GetSection(It.IsAny())).Returns(new Mock().Object);

It worked like a charm for me.

I've found this solution to work reliably for me for my XUnit C# tests & corresponding C# code:

In Controller

string authConnection = this._config["Keys:AuthApi"] + "/somePathHere/Tokens/jwt";

In XUnit Test

Mock<IConfiguration> mockConfig = new Mock<IConfiguration>();
mockConfig.SetupGet(x => x[It.Is<string>(s => s == "Keys:AuthApi")]).Returns("some path here");