Sinon.JS アサーション for Chai
Sinon–Chai は、Sinon.JS のスパイ、スタブ、モッキングフレームワークを Chai アサーションライブラリと共に使用するカスタムアサーションを提供します。Chai のすべての利点と Sinon.JS の強力なツールのすべてを利用できます。
Sinon.JS のアサーションを使用する代わりに
sinon.assert.calledWith(mySpy, "foo");
または、スパイプロパティで 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
同等の構文も利用可能です。
Sinon.JS プロパティ/メソッド | Sinon–Chai アサーション |
---|---|
呼び出し済み | spy.should.have.been.called |
呼び出し回数 | spy.should.have.callCount(n) |
1回呼び出し済み | spy.should.have.been.calledOnce |
2回呼び出し済み | spy.should.have.been.calledTwice |
3回呼び出し済み | spy.should.have.been.calledThrice |
前に呼び出し済み | spy1.should.have.been.calledBefore(spy2) |
後に呼び出し済み | spy1.should.have.been.calledAfter(spy2) |
直前に呼び出し済み | spy.should.have.been.calledImmediatelyBefore(spy2) |
直後に呼び出し済み | spy.should.have.been.calledImmediatelyAfter(spy2) |
new 演算子で呼び出し済み | spy.should.have.been.calledWithNew |
常に new 演算子で呼び出し済み | spy.should.always.have.been.calledWithNew |
コンテキストで呼び出し済み | spy.should.have.been.calledOn(context) |
常にコンテキストで呼び出し済み | spy.should.always.have.been.calledOn(context) |
引数で呼び出し済み | spy.should.have.been.calledWith(…args) |
常に引数で呼び出し済み | spy.should.always.have.been.calledWith(…args) |
1回引数で呼び出し済み | spy.should.always.have.been.calledOnceWith(…args) |
完全に一致する引数で呼び出し済み | spy.should.have.been.calledWithExactly(…args) |
常に完全に一致する引数で呼び出し済み | spy.should.always.have.been.calledWithExactly(…args) |
1回完全に一致する引数で呼び出し済み | spy.should.always.have.been.calledOnceWithExactly(…args) |
一致する引数パターンで呼び出し済み | spy.should.have.been.calledWithMatch(…args) |
常に一致する引数パターンで呼び出し済み | spy.should.always.have.been.calledWithMatch(…args) |
返却値 | spy.should.have.returned(returnVal) |
常に返却値 | spy.should.have.always.returned(returnVal) |
例外発生 | spy.should.have.thrown(errorObjOrErrorTypeStringOrNothing) |
常に例外発生 | spy.should.have.always.thrown(errorObjOrErrorTypeStringOrNothing) |
各アサーションの動作の詳細については、対応するスパイメソッドのドキュメントを参照してください。これらは、スパイだけでなく、個々のスパイ呼び出し、スタブ、モックにも機能します。
Chai の .not
を使用して、任意のアサーションを否定できます。例:notCalled
の場合は spy.should.have.not.been.called
を使用します。
assert
インターフェースの場合は、このライブラリは必要ありません。Sinon.JS アサーション を expose
を使用して 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 のファイルをそれぞれ "chai"
と "sinon-chai"
のモジュールIDにマップするように設定していることを前提として、次のように使用できます。
define(function (require, exports, module) {
var chai = require("chai");
var sinonChai = require("sinon-chai");
chai.use(sinonChai);
});
<script>
タグ
Chai 自体のもの以降に <script>
タグで Sinon–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 によって、Rails アセットパイプラインと統合された Sinon–Chai のRuby gem が提供されるようになりました!