chai-changes

Build Status NPM Version

chai-changes 是对 chai 断言库的扩展,提供了一组针对变化的断言。

断言

所有断言都使用 when 机制。

使用 ‘expect’

expect(-> codeThatYieldsAChangedResult).to....when ->
  executeTheCodeThatCausesTheChange()

expect 部分内的代码将首先执行,然后 when 部分内的代码将执行,然后 expect 部分内的代码将再次执行,并断言差异。

使用 ‘should’ 的相同测试

(-> codeThatYieldsAChangedResult).should....when ->
  executeTheCodeThatCausesTheChange()

when

执行构建预置条件和后置条件。首先执行预置条件,然后执行提供的回调函数。回调函数执行后,它将断言后置条件。它将断言链中的对象更改为回调函数的结果。

Promises

当回调函数返回一个 promise 时,当 promise 完成时会执行后置条件。

when 块的结果是 promise 时,when 语句将返回一个断言 promise。要在 mocha 中使用,可以指定一个 notify 键来触发 done()

expect(-> value).to.change.when(
  -> promise
  notify: done
)

如果你使用 mocha-as-promised,你不需要指定此 notify 方法

另一个选择是

expect(-> value).to.change.when(-> promise).then
  -> done()
  (error) -> done(error)

change

断言当执行 ‘when’ 时,‘expect/should’ 是否改变其结果

result = 0
(-> result).should.change.when -> result += 1
expect(-> result).to.change.when -> result -= 1
expect(-> result).not.to.change.when -> result = result * 1

change.by(delta)

断言 ‘expect/should’ 的变化是否具有提供的 delta

result = 0
(-> result).should.change.by(3).when -> result += 3
expect(-> result).not.to.change.by(-3).when -> result += 1
expect(-> result).to.change.by(-2).when -> result -= 2

change.from(startValue)

断言变化是否从某个值开始。使用深度相等比较值。

result = ['a', 'b']
(-> result).should.change.from(['a', 'b']).when -> result.push('c')
(-> result).should.change.from(['a', 'b']).to(['a', 'b', 'c']).when -> result.push('c')

change.to(endValue)

断言变化是否以某个值结束。使用深度相等比较值。

result = ['a', 'b']
(-> result).should.change.to(['a', 'b', 'c']).when -> result.push('c')
(-> result).should.change.from(['a', 'b']).to(['a', 'c']).when -> result = ['a', 'c']

increase

断言当执行操作时,值是否增加

result = 0
expect(-> result).to.increase.when -> result += 1
expect(-> result).not.to.increase.when -> result
expect(-> result).not.to.increase.when -> result -= 1

decrease

断言当执行操作时,值是否减少

result = 0
expect(-> result).to.decrease.when -> result -= 1
expect(-> result).not.to.decrease.when -> result
expect(-> result).not.to.decrease.when -> result += 1

atLeast(amount)

断言值的变化量是否至少为 ‘amount’

result = 0
expect(-> result).to.change.by.atLeast(4).when -> result += 5
expect(-> result).to.change.by.atLeast(4).when -> result -= 10

atMost(amount)

断言值的变化量是否最多为 ‘amount’

result = 0
expect(-> result).to.change.by.atMost(7).when -> result += 5
expect(-> result).to.change.by.atMost(14).when -> result -= 10

安装和设置

Node

执行 npm install chai-changes 来开始使用。然后

var chai = require("chai");
var chaiChanges = require("chai-changes");

chai.use(chaiChanges);

当然,你可以将这段代码放在一个通用的测试夹具文件中;例如,使用 Mocha

AMD

Chai Changes 支持作为 AMD 模块使用,匿名注册自己(就像 Chai 一样)。因此,假设你已经配置了加载器,将 Chai 和 Chai Changes 文件映射到相应的模块 ID "chai""chai-changes",你可以按如下方式使用它们

define(function (require, exports, module) {
    var chai = require("chai");
    var chaiChanges = require("chai-changes");

    chai.use(chaiChanges);
});

<script> 标签

如果在 Chai 自身之后使用 <script> 标签直接包含 Chai Changes,它将自动插入 Chai 并准备好使用

<script src="chai.js"></script>
<script src="chai-changes.js"></script>

许可证

版权所有 (c) 2012 Matthijs Groen

MIT 许可证(参见 LICENSE 文件)