I have an integration tests project that uses .UseSetting()
in the test class, as follows:
public AccessTokenRetrieval() : base(nameof(AccessTokenRetrieval))
{
var connectionString = GetConnectionString();
var dbSettings = new DbSettings(connectionString);
_userGroupRepository = new UserGroupRepository(dbSettings);
_roleRepository = new RoleRepository(dbSettings);
_userRepository = new UserRepository(dbSettings);
_server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>()
.UseEnvironment("IntegrationTest")
.UseSetting("IntegrationTestConnString", dbSettings.IdentityServerConnectionString));
_handler = _server.CreateHandler();
_client = _server.CreateClient();
}
I would now like to retrieve that setting in the Startup.cs of my actual project. I attempted to do so using:
public void ConfigureIntegrationTestServices(IServiceCollection services)
{
var connectionString = Configuration.GetValue<string>("IntegrationTestConnString");
BuildIdentityServerTests(services, connectionString);
AddCoreServices(services, connectionString);
}
but that seems to return null.
What is the proper way to retrieve this setting?
You could just inject the IConfigurationRoot where you could just use a local config file in the test project. You'd lose the default environment-specific config overrides that are the default in the new project template, but I personally don't use those for configuration management anyway (preferring non-committed appsetting.local.json instead which can be unique to developers and to the CI server).
And for Startup:
Edit: While this does work, it also seems to break
IOptionsSnapshot
configuration injections from recognizing config file changes at run-time. I'm going to leave this answer, but will also keep digging for better way without injecting a custom service just to support test/fixture-specific config overrides.Per-test setting
To pass a setting into a particular
TestHost
instance, you could useConfigureServices
call and override default setting registration.First of all, register your default
DbSettings
instance inStartup.ConfigureServices
method. It's mandatory to useTryAll
call:Register a mocked instance in
WebHostBuilder.ConfigureServices
call:When you try to resolve a
DbSettings
in the application code, you will get a mocked instance. BecauseWebHostBuilder.ConfigureServices
is executed first andTryAdd
call prevents to register a default instance.This hint allows to replace any DI dependency you registered.
Global setting
To set an invariant (accross all tests) setting, set an process-scoped environment variable instead of
UseSetting
call:Then read it:
You need to add environment variable provider to read variables from
Configuration
: