Unit Testing
[Test]
public void Service_Uses_Configuration_Correctly()
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("ApiUrl", "https://test.api"),
new KeyValuePair<string, string>("Timeout", "30")
})
.Build();
var options = Options.Create(config.Get<ApiOptions>());
var service = new ApiService(options);
Assert.That(service.BaseUrl, Is.EqualTo("https://test.api"));
}
Integration Testing
public class TestWebApplicationFactory<TProgram>
: WebApplicationFactory<TProgram> where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
{
config.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("Database:ConnectionString",
"Server=localhost;Database=TestDb;"),
new KeyValuePair<string, string>("ExternalApi:BaseUrl",
"https://mock-api.test")
});
});
}
}