feature(createRoutingFactory): Support use of DI prior to calling createRoutingFactory #475
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
For better or worse, the first invocation of
TestBed.inject
(orTestBed.get
) prevents any further configuration ofProviders
.The Spectator factories of course make
TestBed.inject
calls, but since thecreateXXXFactory
functions also allow overrides for configuringProviders
, Angular Dependency Injection can not be used by application testing code until after the Spectator factory has finished creating the entity under test and returned to it's caller.In many cases this is okay, but if test code needs access to DI based resources before it can call a Spectator factory, then that user land code will have already invoked
TestBed.inject
and therefore the subsequent call to create the desired Spectator factory will fail.This typically manifests as a "Make sure you are not using inject before TestBed" error.
Issue Number: N/A
What is the new behavior?
This PR modifies
createRoutingFactory
to optionally avoid theTestBed.overrideProvider
call during invocation of the factory by configuring the ActivatedRoute during creation of the factory.It maintains backwards compatibility by checking during invocation of the factory to see if the previously configured
ActivatedRoute
needs to be overridden.The TestBed constraint clearly prevents us from having it both ways (pre-factory access to DI, and overrides), but we can provide users the ability to choose one approach or the other.
If you want to supply overrides to the spectator factory, you can't use DI beforehand. But if you require DI availability, then you can choose not to provide overrides to the factory.
This is the same general idea as PR #444 (i.e. Chose pre-factory DI, or override the template).
Does this PR introduce a breaking change?
Other information
I've seen issues and discussions within this project regarding the inability to modify the TestBed after a call to TestBed.compileComponents. It seems that one can indeed modify Providers after TestBed.compileComponents (because this PR works fine). But it looks like you can't make component related modifications after compileComponents.
That was a long winded way of asking if you would be interested in an additional PR documenting some of the configuration nuances between Spectator and TestBed? Since it is clearly a point of confusion for some of us. I'm not a guru, but I'd be happy to do the initial work as long a more experienced person would be willing to review it 😄