用于 RxJS 可观察对象的 Chai 断言
ChaiRx 扩展了 Chai,并提供了一个简单的实用方法 emit
,用于使用 Rx.TestScheduler
测试来自 RxJS 可观察流的事件。
import Rx from 'rx';
import chai from 'chai';
import chaiRx from 'chai-rx';
chai.use(chaiRx);
const { onNext, onCompleted } = Rx.ReactiveTest;
const scheduler = new Rx.TestScheduler();
const xs = scheduler.createHotObservable(
onNext(150, 1),
onNext(210, 2),
onNext(220, 3),
onCompleted(230)
);
// Note we'll start at 200 for subscribe, hence missing the 150 mark
const output = scheduler.startScheduler(() => xs.map(x => x * x), {
created: 100,
subscribed: 200,
disposed: 300
});
expect(output).to.emit([
onNext(210, 4),
onNext(220, 9),
onCompleted(230)
]);
用法
expect
/ should
语法
const xs = scheduler.createHotObservable(onNext(250, { 'foo': 'bar' }), onError(300, new Error('An error')));
const output = scheduler.startScheduler(() => xs);
// expect
expect(output).to.emit([
onNext(250, { 'foo': 'bar' }),
onError(300, ({error}) => error.message === 'An error')
]);
// should
output.should.emit([
onNext(250, { 'foo': 'bar' }),
onError(300, ({error}) => error.message === 'An error')
]);
语言链
const const xs = scheduler.createHotObservable(onNext(250));
const output = scheduler.startScheduler(() => xs);
// with `not`
expect(output).to.not.emit([
onNext(300)
]);
安装
npm install chai-rx
ES6 导入
import chai from 'chai';
import chaiRx from 'chai-rx';
chai.use(chaiRx);
AMD
var chai = require('chai');
var chaiRx = require('chai-rx');
chai.use(chaiRx);
<script>
标签
如果您使用 <script>
标签直接包含 ChaiRx,在 Chai 本身之后,它将自动插入到 Chai 中,并准备使用。
<script src="chai.js"></script>
<script src="chai-rx.js"></script>