chai-changes
chai-changes 是 chai 斷言庫的擴充套件,提供一組針對變更的斷言。
斷言
所有斷言都使用 when
機制。
使用 'expect'
expect(-> codeThatYieldsAChangedResult).to....when ->
executeTheCodeThatCausesTheChange()
expect
區塊內的程式碼會先執行,然後執行 when
區塊中的程式碼,接著再次執行 expect
區塊中的程式碼,並針對差異進行斷言。
使用 'should' 的相同測試
(-> codeThatYieldsAChangedResult).should....when ->
executeTheCodeThatCausesTheChange()
when
執行建立的前置和後置條件。首先執行前置條件,然後執行提供的回呼函式。回呼函式執行後,它會斷言後置條件。它會將斷言鏈中的物件變更為回呼函式的結果。
Promises
當回呼函式返回 promise 時,後置條件會在 promise 實現時執行。
當 when
區塊的結果是 promise 時,when
語句會返回一個斷言 promise。若要在 mocha 中使用,您可以指定 notify
鍵來觸發 done()
expect(-> value).to.change.when(
-> promise
notify: done
)
如果您使用 mocha-as-promised,則不需要指定此通知方法
另一種替代方法是
expect(-> value).to.change.when(-> promise).then
-> done()
(error) -> done(error)
change
斷言 'expect/should' 在執行 'when' 時是否變更其結果
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' 的變更是否具有提供的 delta
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>
標籤
如果您使用 <script>
標籤直接包含 Chai Changes,並在 Chai 本身的標籤之後,它將自動插入 Chai 並準備好使用
<script src="chai.js"></script>
<script src="chai-changes.js"></script>
授權條款
Copyright (c) 2012 Matthijs Groen
MIT 授權條款 (請參閱 LICENSE 檔案)