用于Observable.of()的RxJS first() - 没有顺序的元素 [英] RxJS first() for Observable.of() - no elements in sequence

查看:79
本文介绍了用于Observable.of()的RxJS first() - 没有顺序的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的测试我试图用 Observable.of()模拟一个事件流但是当我尝试

For my tests I am trying to mock an event stream with Observable.of() but when I try

const actions$ = Observable.of({});
...
// in the function that is tested
actions$
  .filter(action => action.type === 'LOAD_REQUEST') 
  .first()
  .subscribe(() => { ... do something });

我收到以下错误


EmptyError:xxx.js中没有元素序列

EmptyError: no elements in sequence in xxx.js

这只发生在我使用 .first()。

如何模拟事件流以使测试不会失败?

How can I mock the event stream so the tests don't fail?

推荐答案

首先会发出一个项目或抛出一个错误,所以在一个项目上调用 .first() empty observable将导致错误。所以这是预期的行为。

First will emit exactly one item or throw an error, so calling .first() on an empty observable will cause an error. So this is the expected behaviour.

如果您想从observable获得最多一个项目,请使用 .take(1)

If you would like to get at most one item from the observable, use .take(1).

这篇关于用于Observable.of()的RxJS first() - 没有顺序的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆