Chai Checkmark

Checkmark 是一个 Chai 插件,用于统计测试期间执行的断言次数。在处理异步测试时,通常难以确定回调是否被实际调用。使用 Checkmark,你可以确保断言已被执行。

安装

在浏览器、CommonJS 模块或 AMD 中包含 Checkmark。

<!-- Browser -->
<script src="chai.js"></script>
<script src="chai-checkmark.js"></script>
// CommonJS
var chai = require("chai"),
    plugin = require("chai-checkmark")
chai.use(plugin)
// AMD
require(["chai", "chai-checkmark"], function(chai, plugin) {
    chai.use(plugin)
})

使用方法

describe("something", function() {
  it("should check two things", function(next) {
    expect(2).checks(next) // <-- pass in the callback

    "sync test".should.be.a("string").mark() // <-- check 1

    setTimeout(function() {
      // check 2, callback is called after the current event finishes
      "async test".should.be.a("string").mark()
    }, 500)
  })
})

API

Checkmark 在 Chai 的断言库的基础上添加了两个方法

expect(Number).check(Function).checks(Function)

这必须在 .mark() 之前调用,但不必在测试的开头调用。你可以指定预期 .mark() 被调用的次数,并可选地传递一个回调函数,在达到标记次数时调用。

expect(str).to.be.a("string").mark()

.mark() 添加到每个你希望被 Checkmark 跟踪的断言的末尾。你可以使用任意数量的 Chai 断言,包括 .and,只要你用 .mark() 结束你的语句。

贡献

欢迎 Pull Request。它们只有在基于 develop 分支的最新版本时才会被合并。如果你落后了,请重新绑定 (不要合并!) 你的更改。要了解为什么重新绑定比合并更好,请查看 Git Rebase 的理由

简而言之,要将你的工作副本更新到 develop 的最新版本,你可以使用 rebase 功能:git pull --rebase。有关详细信息,请参见 Pull with Rebase