【发布时间】:2016-04-30 04:53:22
【问题描述】:
我一直在尝试搜索有关测试 Cycle.js 应用程序的指南,但似乎找不到。有人可以指点我的指南或提供一些示例吗?
【问题讨论】:
标签: javascript testing web reactive-programming cyclejs
我一直在尝试搜索有关测试 Cycle.js 应用程序的指南,但似乎找不到。有人可以指点我的指南或提供一些示例吗?
【问题讨论】:
标签: javascript testing web reactive-programming cyclejs
来自Cycle.js.org:
源和汇可以很容易地用作Adapters and Ports。这也意味着测试主要是提供输入和检查输出。不需要深度嘲讽。您的应用程序只是数据的纯粹转换。
确实,来自 Cycle.js 核心的 GitHub issue,Cycle.js 的作者 André Staltz 解释说:
测试 Cycle.js 代码基本上只是测试 Observables
最简单的测试形式:
// Create the mocked user events
const userEvents = mockDOMSource(...);
// Use them in your tests against `main`
const sinks = main({DOM: userEvents});
sinks.DOM.subscribe(function (vtree) {
// make assertions here on the vtree
});
注意这里我们使用mockDOMSource。
rx.js v5.3.0 发布mockDOMResponse,后来更名为mockDOMSource。这是一个可以轻松模拟用户交互的函数(模拟DOM.select('.foo').events('click') 等意图)。
这是example:
test('CounterButton should increment number by 1 when clicked', t => {
t.plan(4)
const DOM = mockDOMSource({'.inc': {click: Observable.repeat({}, 3)}})
const sinks = CounterButton({DOM})
sinks.DOM
.take(4)
.toArray()
.subscribe(vtrees => {
const counts = vtrees.map(vt => vt.children[0].text.match(/\d+$/)[0])
t.equal(counts[0], '0', 'button has count 0')
t.equal(counts[1], '1', 'button has count 1')
t.equal(counts[2], '2', 'button has count 2')
t.equal(counts[3], '3', 'button has count 3')
})
})
如果您在 GitHub 中全局搜索 mockDOMSource here 和 mockDOMResponse here
那么你可以findsomeexamples进行Cycle.js测试。
您还可以查看Awesome Cycle.js repo 的Testing section。
旁注:很快我们就可以写Marble Tests。不幸的是,目前仅支持 rxjs v4 的 Cycle.js 6 无法做到这一点。 Marble 测试是 rxjs v5 的一个新特性。见:Rxjs testing - is it possible to use marble diagrams also in RxJs 4?
【讨论】:
有本指南以及许多关于循环核心 gitter 的有用开发人员:http://staltz.com/how-to-debug-rxjs-code.html
【讨论】: