我在Angular服务中有一个方法如下,但我不知道如何为这个函数编写单元测试。谁能帮我这个 ?谢谢
message$: Subject<Message> = new Subject<Message>()
getMessage<T>(channel: string): Observable<T> {
return this.message$.asObservable()
.filter(m => m.channel === channel)
.map(m => m.data)
}
这是伪代码。实际代码留作练习。
Create an instance of the service
call getMessage('foo')
subscribe to the returned observable
store every event you receive in a variable receivedEvent
emit a message with the channel 'bar' from the subject
check that receivedEvent is still null
emit a message with the channel 'foo' from the subject
check that receivedEvent contains the data of the message you just emitted