У меня есть следующий код, который я хочу проверить:
@Output() selectedProfile = new EventEmitter<Profile>();
onProfileSelected(profile: profile) {
if (profile) {
this.selectedProfile.emit(profile);
this.router.navigate(
['profiles/' + profile.id],
{relativeTo: this.route}
);
}
}
Тестовая установка:
escribe('ExportProfileSelectorComponent', () => {
let component: ExportProfileSelectorComponent;
let fixture: ComponentFixture<ExportProfileSelectorComponent>;
let routerSpy;
let router;
const profiles: Profile[] = [...mockProfiles];
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [MyComponent],
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
router = TestBed.get(Router);
routerSpy = spyOn(router, 'navigate');
fixture.detectChanges();
});
it('should trigger navigate => {
component.onProfileSelected(profiles[0]);
expect(routerSpy).toHaveBeenCalled();
});
Единственный вывод, который я получаю от Кармы:
Ожидаемая шпионская навигация была вызвана.
Так что на самом деле я не понимаю, почему шпион не вызван.