如何在 rxjs 5 中使用定时间隔测试返回可观察值的函数? [英] How do I test a function that returns an observable using timed intervals in rxjs 5?

查看:51
本文介绍了如何在 rxjs 5 中使用定时间隔测试返回可观察值的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个这样的函数:

For example, I have a function like this:

export function timeRange(start: number, end: number): Rx.Observable<number> {
  return Rx.Observable.interval(1000)
  .map(n => n + start)
  .take(end - start + 1)
}

我有一个单元测试,它运行 timeRange(10, 100) 并针对它断言值.

And I had a unit test that ran timeRange(10, 100) and asserted values against it.

问题是时间间隔会使我的测试太长而无法运行.

The problem is the time intervals would make my test too long to run.

如何在不触及函数本身的情况下缩短时间间隔?

How would you reduce the time interval without touching the function itself?

我尝试阅读有关调度程序的文档,但没有明白.

I tried reading the docs on schedulers but I didn't get it.

推荐答案

有几个注意事项,但您可以按如下方式测试您的功能:

There're a couple of caveats but you can test your function as follows:

const Rx = require('rxjs');
const chai = require('chai');

function timeRange(start, end, interval = 1000, scheduler = Rx.Scheduler.async) {
  return Rx.Observable.interval(interval, scheduler)
    .map(n => n + start)
    .take(end - start + 1)
}

let scheduler = new Rx.TestScheduler(chai.assert.deepEqual);
let source = timeRange(2, 8, 50, scheduler);
let values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8};
scheduler.expectObservable(source).toBe('-----2----3----4----5----6----7----(8|)', values);

scheduler.flush();

需要注意的几点:

  • 该函数现在接受四个参数.我们需要用来传递TestScheduler的间隔和Scheduler.

TestScheduler 需要获取一个方法来深度比较对象.它将用于比较 Notification 对象的数组.

The TestScheduler needs to get a method to deep compare objects. It'll be used to compare arrays of Notification objects.

你不能使用 1000 因为允许的最长时间是 硬编码为 750.请注意,这只是虚拟时间,与实时时间无关,因此我们在此测试中使用的 5050ms 不同.我们只需要适应 750 时间范围.

You can't use 1000 because maximum time allowed is hardcoded as 750. Note, that this is just virtual time and is unrelated to real time so 50 we're using in this test isn't the same as 50ms. We just need to fit inside that 750 time frame.

然后大理石测试作为典型的大理石测试.请参阅 https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md 了解更多信息.请注意,我不能使用与默认 RxJS 运算符相同的全局 mocha 函数.

Then marble tests as typical marble test. See https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md for more information. Just be aware that I can't use the same global mocha functions as the default RxJS operators.

我还必须指定值,因为弹珠测试默认使用字符串,我需要强制使用整数以确保深度相等.

I had to specify also the values because marble tests by default use strings and I need to force integers to ensure deep equality.

您可以通过更改一个弹珠值来测试此操作是否失败:

You can test that this fails by changing one marble value:

let values = {'2': 42, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8};

此外,您可以通过将通知对象打印到控制台来查看它们是什么:

Also, you can see what the notification objects are by printing them to console:

let scheduler = new TestScheduler((actual, expected) => {
   console.log('Actual:', actual, '\n\n', 'Expected:', expected);
   chai.assert.deepEqual(actual, expected);
});

使用 RxJS 5 mocha 助手

Using RxJS 5 mocha helpers

此时 RxJS 5 没有公开 mocha 测试助手.我实际上正在我现在正在处理的另一个项目中使用它(包括生成图表图像).您可以在这里查看 https://github.com/martinsik/rxjs-extrapackage.json 我正在使用什么脚本.它运行良好,但设置有点难以理解.它涉及下载 RxJS 5 存档,复制一些文件并使用 几个文件.然后 mocha 测试的默认选项设置为 mocha.opts 基于 RxJS 5 的原始版本.

At this moment RxJS 5 doesn't expose the mocha testing helpers. I'm actually using it in another project I'm working on right now (including generating diagram images). You can have a look here https://github.com/martinsik/rxjs-extra into package.json what scripts I'm using. It works well but the setup is a little hard to understand. It involves downloading the RxJS 5 archive, copying a few files and patching it with a couple of files. Then the default options for mocha test are set with mocha.opts that is based on the original from RxJS 5.

这篇关于如何在 rxjs 5 中使用定时间隔测试返回可观察值的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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