【发布时间】:2019-11-28 11:42:43
【问题描述】:
我想对下面的函数进行单元测试,看看是否有 n 次 websocket 连接重试
ws = webSocket(url)
notification(action: string) {
return this.ws.asObservable().pipe(
retryWhen(errors =>
errors.pipe(concatMap((_, iteration) =>
timer(Math.pow(2, iteration) * 1000)),
tap(e => console.log('test', e)),
take(10))
),
tap(e => console.log('st', e)),
filter((e: any) => e.action === action));
}
我对如何模拟 websocket 有点困惑
it('should make call 10 times', fakeAsync(() => {
const service = TestBed.get(NotificationService);
let wsSpy = new Subject()
service.ws = wsSpy;
service.filterNotification('test').subscribe(data => {
expect(data).toBe('data')
})
wsSpy.error({ action: 'test' })
tick(time)
wsSpy.error({ action: 'test' })
// sending error the first time cancels the subscription
// Is there any other way to send the error many times
wsSpy.next({ action: 'test' })
}));
【问题讨论】:
标签: angular unit-testing jasmine