【问题标题】:Angular and Rxjs: writing unit test for filtering on SubjectAngular 和 Rxjs:编写用于过滤主题的单元测试
【发布时间】:2018-01-01 22:43:31
【问题描述】:

我在下面的 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】:

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

    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
    

    【讨论】:

    • 非常感谢,我设法编写了测试并且确实有效。我花了一点时间来理解“将您收到的每个事件存储在变量 receivedEvent 中”这一行
    • 我遇到了同样的问题。你能添加你的最终代码吗?
    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 2019-02-15
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 2017-12-20
    相关资源
    最近更新 更多