Answered by:
Override settings used in ConfigureServices, in integration tests

Question
-
User-1092841962 posted
During Startup, in ConfigureServices, I need to access a property from appsettings.json.
I'd like to have it typed, so I'm using the Options pattern.
To retrieve the value, I do this;public void ConfigureServices(IServiceCollection services) {
...
var jwtOptions = configuration.GetSection("Jwt").Get<JwtOptions>();
// then use jwtOptions.MyProperty
...
}The option class looks like this:
public class JwtOptions { public string? MyProperty { get; set; } }
When debugging, all works fine and it gets the correct value from the config file.
Now I want to override the value from the config file during integration tests.
I'm creating my HttpClient like this and overriding the options value in the ConfigureTestServices method.var client = webAppFactory.WithWebHostBuilder(builder => { builder.ConfigureTestServices(services => { services.Configure<JwtOptions>(opts => { opts.MyProperty= "Test value"; }); }); }) .CreateClient(new WebApplicationFactoryClientOptions());
Note: I'm using the Startup file from the webproject when creating the webAppFactory, not using any inheritance.
Unfortunately, the value doesn't get overriden when running the test and the configuration value retrieved is the one that's the web project's appsettings.json file.
What am I doing wrong?
Friday, May 8, 2020 11:25 AM
Answers
-
User-854763662 posted
Hi csharpdev2 ,
For overriding configuration in ASP.NET Core integration tests, you could refer to the below links:
https://blog.markvincze.com/overriding-configuration-in-asp-net-core-integration-tests/
Best Regards,
Sherry
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 9, 2020 7:56 AM -
User-1092841962 posted
I had already looked at those, but overlooked that ConfigureTestServices runs after ConfigureServices, meaning that by that time, the config value has already been fetched and used.
The alternative I used was one of the alternatives that Mark mentioned; the in memory collection;
builder.ConfigureAppConfiguration((context, configBuilder) => { configBuilder.AddInMemoryCollection( new Dictionary<string, string> { ["Jwt:MyProperty"] = "Test value" }); });
It's a shame that the options pattern isn't supported in this scenario. Hopefully the ASP.NET team will add support for it later on.
Thx!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 9, 2020 6:41 PM
All replies
-
User-854763662 posted
Hi csharpdev2 ,
For overriding configuration in ASP.NET Core integration tests, you could refer to the below links:
https://blog.markvincze.com/overriding-configuration-in-asp-net-core-integration-tests/
Best Regards,
Sherry
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 9, 2020 7:56 AM -
User-1092841962 posted
I had already looked at those, but overlooked that ConfigureTestServices runs after ConfigureServices, meaning that by that time, the config value has already been fetched and used.
The alternative I used was one of the alternatives that Mark mentioned; the in memory collection;
builder.ConfigureAppConfiguration((context, configBuilder) => { configBuilder.AddInMemoryCollection( new Dictionary<string, string> { ["Jwt:MyProperty"] = "Test value" }); });
It's a shame that the options pattern isn't supported in this scenario. Hopefully the ASP.NET team will add support for it later on.
Thx!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 9, 2020 6:41 PM