在ngOnInit上,我从数据库中获取用户权限。我无法测试订阅响应中的代码
component.ts:
subscription$: Subject<boolean> = new Subject<boolean>();
ngOnInit() {
this.permissionsService.getUserPermissions().pipe(takeUntil(this.subscription$)).subscribe(res => {
this.nodeTreeInitalization() // this never gets invoked
});
}
component.spec.ts:
beforeEach(() => {
spectator = createComponent();
permissionService = spectator.inject<PermissionService>(PermissionService);
permissionService.getUserPermissions.and.returnValue(timer(0).pipe(
mapTo({permissions: 3})
));
});
it('should call nodeTreeInitalization', () => {
const nodeTreeInitSpy = spyOn(spectator.component, 'nodeTreeInitalization');
spectator.fixture.detectChanges();
expect(nodeTreeInitSpy).toHaveBeenCalled(); // Fails
});
据我所知,我在模拟permissionService.getUserPermissions
,然后通过detectChanges()
调用ngOnInit,所以我不明白为什么我无法测试响应回调