Sinon.JS 的 Chai 斷言

Sinon–Chai 提供了一組自訂斷言,用於將 Sinon.JS 的間諜 (spy)、存根 (stub) 和模擬 (mock) 框架與 Chai 斷言庫一起使用。您將獲得 Chai 的所有優點以及 Sinon.JS 的所有強大工具。

不再使用 Sinon.JS 的斷言

sinon.assert.calledWith(mySpy, "foo");

或笨拙地嘗試在間諜屬性上使用 Chai 的 shouldexpect 介面

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 的對等語法。

Sinon.JS 屬性/方法 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)

有關每個斷言的行為的更多資訊,請參閱相應的間諜方法的文件。這些當然不僅適用於間諜,也適用於個別間諜調用、存根和模擬。

請注意,您可以使用 Chai 的 .not 來否定任何斷言。例如,對於 notCalled,請使用 spy.should.have.not.been.called

對於 assert 介面,則不需要此程式庫。您可以使用 exposeSinon.JS 斷言直接安裝到 Chai 的 assert 物件中

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 install --save-dev sinon-chai 來開始使用。然後

var chai = require("chai");
var sinonChai = require("sinon-chai");

chai.use(sinonChai);

您當然可以將此程式碼放在常見的測試固定裝置檔案中;如需使用 Mocha 的範例,請參閱 Sinon–Chai 測試本身

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,現在有 一個 Ruby gem,其中包含 Sinon–Chai,可將其與 Rails 資產管道整合!