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

时间:2018-01-01 22:43:31

标签: angular unit-testing jasmine rxjs angular2-services

我在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)
}

1 个答案:

答案 0 :(得分: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