测试rxjs的正确方法 [英] The right way to test rxjs

查看:172
本文介绍了测试rxjs的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把这本书"rxjs付诸实践" 并完成了测试部分.

I brought the book "rxjs in action" and just finish the testing section.

测试rxjs代码与通常的测试不同,因为所有内容都是延迟加载.

Testing rxjs codes are different then usual testing, because everything are lazy loading.

在书中,他们提到了两种测试方法,要么传递完了(我正在使用QUnit,并且完成了异步信号的完成信号)或大理石图.

In the book, they mention two test method, either passing done(I am using QUnit and done signals async code is finish) or marble diagrams.

我的问题是,我上面已经提到应该选择哪种方法?

My question is, which method should I choose, that I have mentioned above?

推荐答案

我从同事那里得到了很多这个问题.我终于找到文档我的方式在我的博客上测试RxJ .由于您的问题似乎与RxJs5有关,因此在此仅引用我帖子的相关部分.

I have been getting this question a lot from my colleagues. I finally got around to document my ways of testing RxJs on my blog. Since your question seems to be related to RxJs5 i will only quote the relevant part of my post here.

当您将代码库从RxJs4迁移到5时,您会发现很多东西已经被移动,重命名,并且最重要的是TestScheduler的实现不再可用. RxJ的贡献者kwonoj创建了兼容性填充程序,以帮助向RxJs5迁移..您可以使用npm npm install @kwonoj/rxjs-testscheduler-compat进行安装.并非所有TestScheduler功能都已实现,但最重要的.startScheduler正在运行.

When you migrate your codebase from RxJs4 towards 5 you will find out that a lot of things have been moved, renamed and above all that the implementation of the TestScheduler is no longer available. RxJs contributor kwonoj has created a compatibility shim to help migration towards RxJs5. You can install it using npm npm install @kwonoj/rxjs-testscheduler-compat. Not all features of the TestScheduler are implemented but the most important .startScheduler is working.

const TestScheduler = require('@kwonoj/rxjs-testscheduler-compat').TestScheduler;
const next = require('@kwonoj/rxjs-testscheduler-compat').next;
const complete = require('@kwonoj/rxjs-testscheduler-compat').complete;

it('works in RxJs5 with the compat package', () => {
  const scheduler = new TestScheduler(); // Note; no longer the Rx.TestScheduler

  const results = scheduler.startScheduler(
    () => Rx.Observable.interval(100, scheduler).take(3),
    { created: 100, subscribed: 200, unsubscribed: 1000 } // NOTE: disposed is now renamed to unsubscribed
  );

  collectionAssert.assertEqual(res.messages, [
    next(200 + 100, 0),
    next(200 + 200, 1),
    next(200 + 300, 2),
    complete(200 + 300)
  ]);
});

使用新的Marble测试语法在RxJs5中进行测试

RxJs团队引入了

Testing in RxJs5 using the new Marble testing syntax

The RxJs team has introduced marble testing syntax to more visually define how your operator or custom code should operate.

var e1 = hot('----a--^--b-------c--|');
var e2 = hot(  '---d-^--e---------f-----|');
var expected =      '---(be)----c-f-----|';

expectObservable(e1.merge(e2)).toBe(expected);

在撰写本文时,他们还没有真正在RxJs5库本身之外使用这种方法.有可用的实现以了解如何做它自己.您还可以在 RxJs5的代码库中浏览了解如何设置测试框架以进行自己的大理石测试.公开的有关记录RxJs5测试的问题.我尚未成功设置测试框架以这种方式进行大理石测试.

At the time of writing this post they have not yet made this approach really easy to use outside of the RxJs5 library itself. There are implementations available to see how to do it yourself. You can also look around in the codebase of RxJs5 to see how to setup your testing framework to do your own marble tests. There is an open issue about documenting testing with RxJs5. I have not yet succeeded to get my testing framework setup to do marble testing in this way.

这篇关于测试rxjs的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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