我有一些函数接受我要测试的RxJS主题(支持套接字)。我想以非常要求的回复方式嘲笑这个主题。由于我不确定干净的Rx方法,我很想使用EventEmitter来形成我的假插座。
一般来说,我想:
observer.next(resp)
我确实需要能够使用消息中的数据来形成响应。
正在测试的代码是
export function acquireKernelInfo(sock) {
// set up our JSON payload
const message = createMessage('kernel_info_request');
const obs = shell
.childOf(message)
.ofMessageType('kernel_info_reply')
.first()
.pluck('content', 'language_info')
.map(setLanguageInfo)
.publishReplay(1)
.refCount();
sock.next(message);
return obs;
}
答案 0 :(得分:1)
您可以手动创建两个主题并将它们粘合在一起"作为一个Subject.create
的主题:
const sent = new Rx.Subject();
const received = new Rx.Subject();
const mockWebSocketSubject = Subject.create(sent, received)
const s1 = sent.subscribe(
(msg) => sentMsgs.push({ next: msg }),
(err) => sentMsgs.push({ error: err }),
() => sendMsgs.push({ complete: true })
);
const s2 = recieved.subscribe(
(msg) => sentMsgs.push({ next: msg }),
(err) => sentMsgs.push({ error: err }),
() => sendMsgs.push({ complete: true })
);
// to send a message
// (presumably whatever system you're injecting this into is doing the sending)
sent.next('weee');
// to mock a received message
received.next('blarg');
s1.unsubscribe();
s2.unsubscribe();
那就是说,这真的是你正在测试什么,它是如何构建的,以及API是什么。
理想情况下,您可以同步运行整个测试。如果您无法解决某些与Rx相关的原因,那么您应该查看TestScheduler
,它具有在虚拟化时间内运行测试的功能。