按顺序 Sinon-Chai 断言

Build Status

Dependency Status devDependency Status

动机

Sinon-ChaiSinon.JS 提供 Chai 断言。不幸的是,它没有处理确保间谍以特定顺序被调用多次。这会导致笨拙、不流畅的断言。

var spy = sinon.spy();
[1, 2, 3].forEach(spy);
expect(spy.getCall(0).args[0]).to.equal(1);
expect(spy.getCall(1).args[0]).to.equal(2);
expect(spy.getCall(2).args[0]).to.equal(3);

使用 sinon-chai-in-order,你可以这样说:

expect(spy).inOrder.to.have.been.calledWith(1)
                   .subsequently.calledWith(2)
                   .subsequently.calledWith(3);

设置

在 Node 中,只需使用 npm 安装

$ npm install sinon-chai-in-order

在你的测试中,让 Chai 使用这个插件。确保你也使用了 sinon-chai,否则嵌套断言将无法正常工作。

import chai, {expect} from 'chai';
import sinonChai from 'sinon-chai';
import sinonChaiInOrder from 'sinon-chai-in-order';

chai.use(sinonChai);
chai.use(sinonChaiInOrder);

这个插件以 UMD 格式发布,因此你可以在任何地方使用它。但是,它被导出为 ES6 模块。如果使用 ES5,请使用

chai.use(require('chai-react-element').default);

贡献

设置

这个项目使用 Gulp 进行构建和测试,使用 webpack-dev-server 进行浏览器内运行和调试。要安装项目,只需运行 npm install。

要启动开发环境,运行 npm start,或者,如果你已经全局安装了 Gulp,则运行 gulp dev。这将使用 Mocha 运行测试,并且还会在端口 8080 上启动 webpack-dev-server。要运行测试,请使用 npm test(或 gulp test)。

问题

对于你遇到的任何问题,请在项目的 GitHub 库中打开一个问题。请在问题中讨论你的问题,不要创建拉取请求。

拉取请求

请尝试使用测试驱动开发来开发你的提交。至少,确保你的更改有很好的测试覆盖,并且你的代码是干净的。