用于 Chai 的 Sinon.JS 断言
Sinon-Chai 为使用 Sinon.JS 的 spy、stub 和 mock 框架提供了一套自定义的断言,其中包含了 Chai 断言库。你可以通过 Sinon.JS 的所有强大的工具获得 Chai 的所有好处。
代替使用 Sinon.JS 的断言。
sinon.assert.calledWith(mySpy, "foo");
或者笨拙地试图在 spy 属性上使用 Chai 的 should 或 expect 接口。
mySpy.calledWith("foo").should.be.ok; expect(mySpy.calledWith("foo")).to.be.ok;
你可以这样
mySpy.should.have.been.calledWith("foo"); expect(mySpy).to.have.been.calledWith("foo");
断言
所有你喜欢的 Sinon.JS 断言都进入了 Sinon-Chai。我们在这里展示了 should 语法,同时也提供了 expect equivalent。
Sinon.JS property/method | Sinon–Chai 断言 |
---|---|
called | spy.should.have.been.called |
callCount | spy.should.have.callCount(n) |
calledOnce | spy.should.have.been.calledOnce |
calledTwice | spy.should.have.been.calledTwice |
calledThrice | spy.should.have.been.calledThrice |
calledBefore | spy1.should.have.been.calledBefore(spy2) |
calledAfter | spy1.should.have.been.calledAfter(spy2) |
calledImmediatelyBefore | spy.should.have.been.calledImmediatelyBefore(spy2) |
calledImmediatelyAfter | spy.should.have.been.calledImmediatelyAfter(spy2) |
calledWithNew | spy.should.have.been.calledWithNew |
alwaysCalledWithNew | spy.should.always.have.been.calledWithNew |
calledOn | spy.should.have.been.calledOn(context) |
alwaysCalledOn | spy.should.always.have.been.calledOn(context) |
calledWith | spy.should.have.been.calledWith(...args) |
alwaysCalledWith | spy.should.always.have.been.calledWith(...args) |
calledOnceWith | spy.should.always.have.been.calledOnceWith(...args) |
calledWithExactly | spy.should.have.been.calledWithExactly(...args) |
alwaysCalledWithExactly | spy.should.always.have.been.calledWithExactly(...args) |
calledOnceWithExactly | spy.should.always.have.been.calledOnceWithExactly(...args) |
calledWithMatch | spy.should.have.been.calledWithMatch(...args) |
alwaysCalledWithMatch | spy.should.always.have.been.calledWithMatch(...args) |
returned | spy.should.have.returned(returnVal) |
alwaysReturned | spy.should.have.always.returned(returnVal) |
threw | spy.should.have.thrown(errorObjOrErrorTypeStringOrNothing) |
alwaysThrew | spy.should.have.always.thrown(errorObjOrErrorTypeStringOrNothing) |
关于每个断言行为的更多信息,请参见相应的 spy 方法的文档。当然,这些方法不仅适用于间谍,还适用于单个侦探、打桩和 mock 调用。
注意,你可以用 Chai 的 .not 来否定任何断言。如:对于 notCalled 使用 spy.should.have.not.been.called。
对于断言接口,没有必要使用这个库。你可以通过 expose 将 Sinon.JS 断言 直接安装到 Chai 的断言对象中。
var chai = require("chai"); var sinon = require("sinon"); sinon.assert.expose(chai.assert, { prefix: "" });
示例
使用 Chai should:
"use strict"; var chai = require("chai"); var sinon = require("sinon"); var sinonChai = require("sinon-chai"); chai.should(); chai.use(sinonChai); function hello(name, cb) { cb("hello " + name); } describe("hello", function () { it("should call callback with correct greeting", function () { var cb = sinon.spy(); hello("foo", cb); cb.should.have.been.calledWith("hello foo"); }); });
使用 Chai expect:
"use strict"; var chai = require("chai"); var sinon = require("sinon"); var sinonChai = require("sinon-chai"); var expect = chai.expect; chai.use(sinonChai); function hello(name, cb) { cb("hello " + name); } describe("hello", function () { it("should call callback with correct greeting", function () { var cb = sinon.spy(); hello("foo", cb); expect(cb).to.have.been.calledWith("hello foo"); }); });
安装和使用
Node
执行 npm --save-dev sinon-chai 来启动和运行。然后:
var chai = require("chai"); var sinonChai = require("sinon-chai"); chai.use(sinonChai);
当然,你也可以把这些代码放在一个普通的测试夹具文件中;关于使用 Mocha 的例子,请看 the Sinon–Chai 测试本身。
define(function (require, exports, module) { var chai = require("chai"); var sinonChai = require("sinon-chai"); chai.use(sinonChai); });
AMD
Sinon-Chai 支持作为 AMD 模块使用,匿名注册自己(就像 Chai 一样)。所以,假设你已经配置了你的加载器,将 Chai 和 Sinon-Chai 文件映射到各自的模块 ID "chai" 和 "sinon-chai" 上,你可以按照以下方式使用它们。
define(function (require, exports, module) { var chai = require("chai"); var sinonChai = require("sinon-chai"); chai.use(sinonChai); });
<script>标签
如果你直接用 <script> 标签将 Sinon-Chai 包含在 Chai 本身的标签之后,那么它就会自动插入到 Chai 中,并且可以使用。请注意,你也需要获得最新的 Sinon.JS 浏览器版本。
<script src="chai.js"></script> <script src="sinon-chai.js"></script> <script src="sinon.js"></script>
Ruby on Rails
感谢 Cymen Vig,现在有了 Sinon-Chai 的 Ruby gem,可以将其与 Rails asset pipeline 集成在一起。
(The first version translated by vz on 2020.08.30)