Comments (7)
@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:
from alba.
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.
from alba.
"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.
from alba.
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.
from alba.
Then again, I can just inject stuff then make sure I only use TryAdd
methods.
from alba.
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
.
aspnet/Hosting#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
from alba.
Closing this as there actually are some better facilities to deal with this issue now.
from alba.
Related Issues (20)
- How to setup properly X-XSRF-TOKEN header for ValidateAntiForgeryToken? HOT 1
- HttpContext RequestServices is null HOT 3
- Some issues with minimal api HOT 3
- HttpResponseBody.ReadAsText() hangs (IScenarioResult) HOT 3
- TokenValidationParameters for JwtSecurityStub
- Alba's built in JSON serialization isn't working for oData endpoints HOT 2
- InvalidOperationException throw in HeaderMultiValueAssertion HOT 1
- Inconsistent behavior when using AlbaHost.For HOT 1
- Does this look correct: a ContentShouldBeJson assertion helper HOT 2
- Getting AuthenticationStub to override multiple schemas HOT 1
- Suggestion: Include a way to stub a specific authentication schema
- Adjust IAlbaExtension signature to use IWebHostBuilder instead of IHostBuilder
- Revisit the usage of ScenarioAssertionException
- Why the container lifetime difference between AlbaHost ctor and task based HOT 1
- Move to JsonWebToken within JwtSecurityStub for .NET 8+
- Error Log Message when running within Docker context HOT 1
- Consider documentation/workarounds for minimal API configuration problems
- `required` properties will trigger a `AlbaJsonFormatterException` when requesting a nested object HOT 3
- Alba seems to run forever un mstest? HOT 2
- [feat] Allow removing claim value in test? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alba.