Код файла .ts
@Component({
selector: 'app-nav-breadcrumbs',
templateUrl: './nav-breadcrumbs.component.html',
styleUrls: ['./nav-breadcrumbs.component.scss']
})
export class NavBreadcrumbsComponent implements OnInit, OnDestroy {
private subscriptions = new Subscription();
constructor(
private _router: Router
) {
}
ngOnInit() {
this.subscriptions.add(
this._router.events.pipe(
filter(event => event instanceof NavigationEnd),
distinctUntilChanged(),
map(event => this.myMethod())
).subscribe({
next: (res) => {
// ...
},
error: (e) => {
console.warn(e);
}
})
);
}
ngOnDestroy() {
this.subscriptions.unsubscribe();
}
myMethod() {
// some logic...
}
}
мой файл спецификаций
describe('NavBreadcrumbsComponent', () => {
let component: NavBreadcrumbsComponent;
let fixture: ComponentFixture<NavBreadcrumbsComponent>;
beforeEach(() => {
const routerStub = {
events: { pipe: () => ({ subscribe: () => ({}) }) },
navigate: () => ({})
};
TestBed.configureTestingModule({
schemas: [NO_ERRORS_SCHEMA],
declarations: [NavBreadcrumbsComponent],
providers: [
{ provide: Router, useValue: routerStub }
]
});
fixture = TestBed.createComponent(NavBreadcrumbsComponent);
component = fixture.componentInstance;
});
it('can load instance', () => {
expect(component).toBeTruthy();
});
describe('ngOnInit', () => {
it('makes expected calls', () => {
spyOn(component, 'myMethod');
component.ngOnInit();
expect(component.myMethod).toHaveBeenCalled();
});
});
});
В настоящее время получается ошибка ниже
NavBreadcrumbsComponent ngOnInit makes expected calls
Expected spy myMethod to have been called.
Error: Expected spy myMethod to have been called.
at stack (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2455:17)
at buildExpectationResult (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2425:14)
at Spec.expectationResultFactory (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:901:18)
at Spec.addExpectationResult (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:524:34)
at Expectation.addExpectationResult (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:845:21)
at Expectation.toHaveBeenCalled (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2369:12)
at UserContext.eval (webpack:///./src/app/core/components/nav-breadcrumbs/nav-breadcrumbs.component.spec.ts?:45:47)
at ZoneDelegate.invoke (webpack:///./node_modules/zone.js/dist/zone.js?:390:26)
at ProxyZoneSpec.onInvoke (webpack:///./node_modules/zone.js/dist/zone-testing.js?:288:39)
at ZoneDelegate.invoke (webpack:///./node_modules/zone.js/dist/zone.js?:389:52)
В настоящее время я не могу шпионить и протестировать метод myMethod () toHaveBeenCalled. Поскольку он вызывается внутри функции .map () и, в свою очередь, находится внутри функции .pipe (), я не могу перехватить вызов метода извне. Пожалуйста, предложите, как шпионить и тестировать метод myMethod (просто ожидать (myMethod) .toHaveBeenCalled ())