I am new to writing test specs using jasmine. I have a condition in ngOnInit() something like, router.url.includes(some string). The default spec test case which checks component to be truthy is getting failed saying TypeError: Cannot find includes of undefined. Is there any way to spy or mock includes?
Asked
Active
Viewed 1,801 times
2
-
https://stackoverflow.com/a/39793256/7365461 You can mock the router or try using RouterTestingModule. – AliF50 Apr 19 '21 at 16:52
1 Answers
4
You can use RouterTestingModule
to mock the angular router.
Just import the angular router as below.
import { RouterTestingModule } from '@angular/router/testing';
import { ActivatedRoute, Router } from '@angular/router';
And add it to the imports
array of NgModule
TestBed.configureTestingModule({
declarations: [
someComponent
],
imports: [
RouterTestingModule.withRoutes([]),
],
providers: [someProvider],
}).compileComponents
Then you can inject the Router as follows and use it for testing
it('should Navigate', () => {
const actiavtedRoute = TestBed.inject(ActivatedRoute);
const router = TestBed.inject(Router);
})

Mohana Naga Venkat Sayempu
- 866
- 6
- 11