Angular 7 TestBed.overrideProvider does not work for ActivateRoute












0















I'm using Angular 7 w/ Jest and am mocking out providers to components. There are lots of times where I need to change what is injected into the components after the TestBed is compiled, and I've had no problems doing that with this code:



TestBed.configureTestingModule({...});
await TestBed.compileComponents();

... some code
TestBed.overrideProvider(SecurityService, {useValue: mockSecurityService});
fixture = TestBed.createComponent(LoginComponent);
fixture.detectChanges()


When I do this, the NEW mockSecurityService is included in my component, exactly as I would expect. But... and this is really weird... this method does NOT work for ActivatedRoute, no matter what I do. So if I change the mockActivatedRoute and add this line of code:



TestBed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute});
TestBed.overrideProvider(SecurityService, {useValue: mockSecurityService});
fixture = TestBed.createComponent(LoginComponent);
fixture.detectChanges()


The component is instantiated with the first mockActivatedRoute class that it got, and no matter how many times overrideProvider is called afterward, the first object gets stands.



This is really weird because the exact same code path works for my mockSecurityService as well as a mock of the Angular Router service. Is there something about the ActivatedRoute class that is causing the TestBed to ignore overrides? I stepped through the code and can see no difference; as far as I can tell, the overrideProviderMap is getting the ActivatedRoute override, but the component instantiation is ignoring it nonetheless.










share|improve this question

























  • does the problem still remain if you chain on the same TestBed call? Testbed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute}).overrideProvider(SecurityService, {useValue: mockSecurityService});

    – Lucho
    Nov 24 '18 at 22:11













  • You can override providers in TestBed.configureTestingModule part.

    – Alexander Poshtaruk
    Nov 25 '18 at 11:32











  • Can you share the ActivatedRoute sample code?

    – Sree.Bh
    Dec 18 '18 at 13:53
















0















I'm using Angular 7 w/ Jest and am mocking out providers to components. There are lots of times where I need to change what is injected into the components after the TestBed is compiled, and I've had no problems doing that with this code:



TestBed.configureTestingModule({...});
await TestBed.compileComponents();

... some code
TestBed.overrideProvider(SecurityService, {useValue: mockSecurityService});
fixture = TestBed.createComponent(LoginComponent);
fixture.detectChanges()


When I do this, the NEW mockSecurityService is included in my component, exactly as I would expect. But... and this is really weird... this method does NOT work for ActivatedRoute, no matter what I do. So if I change the mockActivatedRoute and add this line of code:



TestBed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute});
TestBed.overrideProvider(SecurityService, {useValue: mockSecurityService});
fixture = TestBed.createComponent(LoginComponent);
fixture.detectChanges()


The component is instantiated with the first mockActivatedRoute class that it got, and no matter how many times overrideProvider is called afterward, the first object gets stands.



This is really weird because the exact same code path works for my mockSecurityService as well as a mock of the Angular Router service. Is there something about the ActivatedRoute class that is causing the TestBed to ignore overrides? I stepped through the code and can see no difference; as far as I can tell, the overrideProviderMap is getting the ActivatedRoute override, but the component instantiation is ignoring it nonetheless.










share|improve this question

























  • does the problem still remain if you chain on the same TestBed call? Testbed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute}).overrideProvider(SecurityService, {useValue: mockSecurityService});

    – Lucho
    Nov 24 '18 at 22:11













  • You can override providers in TestBed.configureTestingModule part.

    – Alexander Poshtaruk
    Nov 25 '18 at 11:32











  • Can you share the ActivatedRoute sample code?

    – Sree.Bh
    Dec 18 '18 at 13:53














0












0








0








I'm using Angular 7 w/ Jest and am mocking out providers to components. There are lots of times where I need to change what is injected into the components after the TestBed is compiled, and I've had no problems doing that with this code:



TestBed.configureTestingModule({...});
await TestBed.compileComponents();

... some code
TestBed.overrideProvider(SecurityService, {useValue: mockSecurityService});
fixture = TestBed.createComponent(LoginComponent);
fixture.detectChanges()


When I do this, the NEW mockSecurityService is included in my component, exactly as I would expect. But... and this is really weird... this method does NOT work for ActivatedRoute, no matter what I do. So if I change the mockActivatedRoute and add this line of code:



TestBed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute});
TestBed.overrideProvider(SecurityService, {useValue: mockSecurityService});
fixture = TestBed.createComponent(LoginComponent);
fixture.detectChanges()


The component is instantiated with the first mockActivatedRoute class that it got, and no matter how many times overrideProvider is called afterward, the first object gets stands.



This is really weird because the exact same code path works for my mockSecurityService as well as a mock of the Angular Router service. Is there something about the ActivatedRoute class that is causing the TestBed to ignore overrides? I stepped through the code and can see no difference; as far as I can tell, the overrideProviderMap is getting the ActivatedRoute override, but the component instantiation is ignoring it nonetheless.










share|improve this question
















I'm using Angular 7 w/ Jest and am mocking out providers to components. There are lots of times where I need to change what is injected into the components after the TestBed is compiled, and I've had no problems doing that with this code:



TestBed.configureTestingModule({...});
await TestBed.compileComponents();

... some code
TestBed.overrideProvider(SecurityService, {useValue: mockSecurityService});
fixture = TestBed.createComponent(LoginComponent);
fixture.detectChanges()


When I do this, the NEW mockSecurityService is included in my component, exactly as I would expect. But... and this is really weird... this method does NOT work for ActivatedRoute, no matter what I do. So if I change the mockActivatedRoute and add this line of code:



TestBed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute});
TestBed.overrideProvider(SecurityService, {useValue: mockSecurityService});
fixture = TestBed.createComponent(LoginComponent);
fixture.detectChanges()


The component is instantiated with the first mockActivatedRoute class that it got, and no matter how many times overrideProvider is called afterward, the first object gets stands.



This is really weird because the exact same code path works for my mockSecurityService as well as a mock of the Angular Router service. Is there something about the ActivatedRoute class that is causing the TestBed to ignore overrides? I stepped through the code and can see no difference; as far as I can tell, the overrideProviderMap is getting the ActivatedRoute override, but the component instantiation is ignoring it nonetheless.







angular jestjs angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 14:25









skyboyer

3,75311229




3,75311229










asked Nov 24 '18 at 20:20









user1543276user1543276

466




466













  • does the problem still remain if you chain on the same TestBed call? Testbed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute}).overrideProvider(SecurityService, {useValue: mockSecurityService});

    – Lucho
    Nov 24 '18 at 22:11













  • You can override providers in TestBed.configureTestingModule part.

    – Alexander Poshtaruk
    Nov 25 '18 at 11:32











  • Can you share the ActivatedRoute sample code?

    – Sree.Bh
    Dec 18 '18 at 13:53



















  • does the problem still remain if you chain on the same TestBed call? Testbed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute}).overrideProvider(SecurityService, {useValue: mockSecurityService});

    – Lucho
    Nov 24 '18 at 22:11













  • You can override providers in TestBed.configureTestingModule part.

    – Alexander Poshtaruk
    Nov 25 '18 at 11:32











  • Can you share the ActivatedRoute sample code?

    – Sree.Bh
    Dec 18 '18 at 13:53

















does the problem still remain if you chain on the same TestBed call? Testbed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute}).overrideProvider(SecurityService, {useValue: mockSecurityService});

– Lucho
Nov 24 '18 at 22:11







does the problem still remain if you chain on the same TestBed call? Testbed.overrideProvider(ActivatedRoute, {useValue: mockActivatedRoute}).overrideProvider(SecurityService, {useValue: mockSecurityService});

– Lucho
Nov 24 '18 at 22:11















You can override providers in TestBed.configureTestingModule part.

– Alexander Poshtaruk
Nov 25 '18 at 11:32





You can override providers in TestBed.configureTestingModule part.

– Alexander Poshtaruk
Nov 25 '18 at 11:32













Can you share the ActivatedRoute sample code?

– Sree.Bh
Dec 18 '18 at 13:53





Can you share the ActivatedRoute sample code?

– Sree.Bh
Dec 18 '18 at 13:53












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462019%2fangular-7-testbed-overrideprovider-does-not-work-for-activateroute%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462019%2fangular-7-testbed-overrideprovider-does-not-work-for-activateroute%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'