How to use mock routes in Angular tests
The Angular Route class takes 8 arguments in the constructor. For a simple test, I didn’t want to construct it by hand. Thus I used the RouterTestingModule. Here you can see how it is done:
describe('ArticleService', () => {
let router: Router;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([
{ path: 'overview', component: OverviewComponent },
]),
],
});
router = TestBed.inject<Router>(Router);
});
describe('addArticle', () => {
it('should navigate to overview page after adding an article', () => {
articleService = new ArticleService(router);
const navigateSpy = spyOn(router, 'navigate');
articleService.addArticle('title');
expect(navigateSpy).toHaveBeenCalledWith(['overview']);
});
});
});
It verifies that the code under test called the navigate method with the page overview.