Skip to content

Instantly share code, notes, and snippets.

@umair-me
Last active July 7, 2022 03:00
Show Gist options
  • Save umair-me/bb23e6d9f367cf2f992ff7126380c667 to your computer and use it in GitHub Desktop.
Save umair-me/bb23e6d9f367cf2f992ff7126380c667 to your computer and use it in GitHub Desktop.
Mock Configuration.GetSection
Mock<IConfiguration> configuration = new Mock<IConfiguration>();
configuration.Setup(c => c.GetSection(It.IsAny<String>())).Returns(new Mock<IConfigurationSection>().Object);
@guojianwei001
Copy link

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

        _configuration = new ConfigurationBuilder()
            .AddInMemoryCollection(inMemorySettings)
            .Build();

`
[()](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0&viewFallbackFrom=aspnetcore-3.0#memory-configuration-provider)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment