How to Unit Test Method Calling IConfiguration.Get<T> extension
The configuration module is independent of web host related features.
You should be able to create an in memory configuration to test against without the need to bind it to a web host.
Review the following example test
public class TestConfig {
[Required]
public string SomeKey { get; set; }
[Required] //<--NOTE THIS
public string SomeOtherKey { get; set; }
}
//...
[Fact]
public void Should_Fail_Validation_For_Required_Key() {
//Arrange
var inMemorySettings = new Dictionary<string, string>
{
{"Email:SomeKey", "value1"},
//{"Email:SomeOtherKey", "value2"}, //Purposely omitted for required failure
//...populate as needed for the test
};
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();
//Act
Action act = () => configuration.GetSection("Email").GetValid<TestConfig>();
//Assert
ValidationException exception = Assert.Throws<ValidationException>(act);
//...other assertions of validation results within exception object
}
This in my opinion would be coming close to an integration test, but ideally you are just using framework dependent features in order to isolate the testing of the extension method.