我如何测试可观察对象的预订块内是否调用了特定功能?
场景如下:
private openMemberPopup(): void {
this.memberDialogRef = this.dialogRef.open(MemberPopupComponent, { width: '800px' });
this.memberDialogRef.componentInstance.onMemberAdded.subscribe(member => {
this.store.dispatch(new CreateMemberAction({ member })); <-- this line should be tested
});
}
我想测试上面的方法,即打开一个材质对话框。然后,我订阅Popup组件的EventEmitter。在下一个回调中,我调度了一个新的NGRX-Action。
以下方法无效:
it('should dispatch CreateMemberAction', async () => {
const dialogRef = component.dialogRef;
// tslint:disable-next-line:no-object-literal-type-assertion
const member = {} as Member;
const spyOnRef = spyOn(dialogRef, 'open').and.returnValue({
componentInstance: {
onMemberAdded: hot('-a-|', { a: member })
}
});
const store = TestBed.get(Store);
const spy = spyOn(store, 'dispatch');
const expected = new CreateMemberAction({ member });
component['openAddMemberPopup']();
expect(spy).toHaveBeenCalledWith(expected);
});
错误消息是:
使用[CreateMemberAction({ 有效负载:Object({成员:Object({})}),类型:'[Member]创建 成员'})],但从未被调用。
有任何提示或最佳做法吗?谢谢