@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)
当前,我无法监视并测试toHaveBeenCalled的myMethod()方法。由于在.map()函数内部被调用,而在.pipe()函数内部被调用,因此我无法从外部捕获方法调用。请建议如何监视和测试myMethod方法(只是期望(myMethod).toHaveBeenCalled())