chai-changes
chai-changes は、chai アサーションライブラリの拡張であり、変更に特化したアサーションのセットを提供します。
アサーション
すべてのアサーションは when
メカニズムを使用します。
‘expect’ の使用
expect(-> codeThatYieldsAChangedResult).to....when ->
executeTheCodeThatCausesTheChange()
expect
セクション内のコードが最初に実行され、次に when
セクションのコードが実行され、その後 expect
セクションのコードが再度実行され、その差異がアサートされます。
‘should’ を使用した同じテスト
(-> codeThatYieldsAChangedResult).should....when ->
executeTheCodeThatCausesTheChange()
when
事前条件と事後条件のビルドアップを実行します。最初に事前条件を実行し、次に提供されたコールバックを実行します。そしてコールバックの後、事後条件をアサートします。アサーションチェーン内のオブジェクトをコールバックの結果に変更します。
Promise
コールバックが Promise を返す場合、Promise が履行されたときに事後条件が実行されます。
when
ステートメントは、when
ブロックの結果が Promise の場合、アサーション Promise を返します。mocha で使用するには、done()
をトリガーする notify
キーを指定できます。
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’ の変更が指定されたデルタを持つかどうかをアサートします。
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 Changes を Chai 自体の <script>
タグの後ろに直接含めると、自動的に Chai にプラグインされ、使用できるようになります。
<script src="chai.js"></script>
<script src="chai-changes.js"></script>
ライセンス
Copyright (c) 2012 Matthijs Groen
MIT ライセンス(LICENSE ファイルを参照)