Angular和Rxjs:写入单元测试以过滤Subject

问题描述 投票:1回答:1

我在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)
}
angular unit-testing jasmine rxjs angular2-services
1个回答
0
投票

这是伪代码。实际代码留作练习。

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
© www.soinside.com 2019 - 2024. All rights reserved.