Closed adamhathcock closed 2 years ago
@adamhathcock I do like that idea and it's something I had intended to get around to. We have that with the older FubuMVC version of all of this that we use at work. The Before()
, After()
stuff that isn't implemented yet was vaguely meant for that.
There's a big caveat though, you'd want to use something like StructureMap's child container functionality so that your fake services injected for a specific test don't bleed into subsequent tests.
If you only need to inject mocks or stubs upfront, there's some hooks on the SystemUnderTest we could use for that, or at least make sure the ServiceCollection is exposed.
There's a couple caveats though:
I guess I would want to Mock per test. I want to intercept SQL and Mock the return values to fully integration test.
So essentially, I'd want to resetup the entire SystemUnderTest
each test. I'd pass my Mocks to the ForStartup
call. By doing this, I wouldn't be worrying about sharing with other tests anyway.
Does this sound like a valid scenario? I feel like I could be misusing something.
"So essentially, I'd want to resetup the entire SystemUnderTest each test" - as a temporary workaround yes, but we'd want something like fubu's model that can isolate service overrides between tests without having to bootstrap all over again. Restarting the app between tests isn't going to scale to any kind of bigger test suite.
"Does this sound like a valid scenario?" -- we do it at work, so it better be valid;-)
I say yes, especially if you have external dependencies you can't control from the test harness. We have a little trouble with that getting overused at work though.
On the surface, I can't do this easily.
WebHostBuilder.ConfigureServices
is always called before the ConfigureServices
on the Startup class. Which makes sense. I'd want to configure services after the Startup class is called I guess.
Then again, I can just inject stuff then make sure I only use TryAdd
methods.
It is indeed true that WebHostBuilder.ConfigureServices
is run before Startup.ConfigureServices
, making it impossible to use for overriding service registrations from Startup. However, since .Net Core 2.1 (I think) there is an extension method ConfigureTestServices
in Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions
that is run after Startup.ConfigureServices
.
https://github.com/aspnet/Hosting/issues/1303
So the Alba docs are wrong, and should be corrected: http://jasperfx.github.io/alba/documentation/bootstrapping/
Change ConfigureServices to ConfigureTestServices here:
var stubbedWebService = new StubbedWebService();
var builder = WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
// override the environment if you need to
.UseEnvironment("Testing")
// override service registrations if you need to
.ConfigureTestServices(s =>
{
s.AddSingleton<IExternalWebService>(stubbedWebService);
});
// Create the SystemUnderTest, and alternatively override what Alba
// thinks is the main application assembly
// THIS IS IMPORTANT FOR MVC CONTROLLER DISCOVERY
var system = new SystemUnderTest(builder, applicationAssembly:typeof(Startup).Assembly);
Or the simpler form:
var system = SystemUnderTest.ForStartup<Startup>(builder =>
{
return builder.ConfigureTestServices(s =>
{
s.AddSingleton<IExternalWebService>(stubbedWebService);
// Alternatively, replace the registration
//s.Replace(ServiceDescriptor.Singleton(stubbedWebService));
});
});
Note that if you override with AddSingleton
(or similar) there will be two registrations for that service (interface), but the latest one is picked when resolving. It feels safer to me to use Replace. :)
Also, the Alba unit test override_service_registration_in_bootstrapping
probably needs to be changed, because I can't see that it actually works as intended since it uses WebHostBuilder.ConfigureServices
:
https://github.com/JasperFx/alba/blob/master/src/Alba.Testing/Acceptance/using_custom_service_registrations.cs
@jeremydmiller
Closing this as there actually are some better facilities to deal with this issue now.
What I'd like to do is inject some mocked classes after the ServiceCollection has been built.
Of course, I could do something like change the HostingEnvironment to know if the environment was called "test" or something to do something else but it feels less elegant.
Would you accept a PR to add an
Action<IServiceCollection>
or something to allow manipulation of the service collection before the service provider is built?