斷言風格
本節指南將介紹您在測試環境中可以使用的三種不同斷言風格。一旦您選擇了其中一種,建議您查看所選風格的 API 文件。
Assert
assert 風格透過 assert
介面公開。這提供了經典的 assert-dot 表示法,類似於 node.js 中包含的。然而,這個 assert 模組提供了幾個額外的測試,並且與瀏覽器相容。
var assert = require('chai').assert
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
assert.typeOf(foo, 'string'); // without optional message
assert.typeOf(foo, 'string', 'foo is a string'); // with optional message
assert.equal(foo, 'bar', 'foo equal `bar`');
assert.lengthOf(foo, 3, 'foo`s value has a length of 3');
assert.lengthOf(beverages.tea, 3, 'beverages has 3 types of tea');
在所有情況下,assert 風格允許您在 assert
陳述式中包含一個可選的訊息作為最後一個參數。如果您的斷言未通過,這些訊息將包含在錯誤訊息中。
BDD
BDD 風格有兩種形式:expect
和 should
。兩者都使用相同的可鏈接語言來建構斷言,但它們在最初建構斷言的方式上有所不同。就 should
而言,還有一些注意事項和額外工具來克服這些注意事項。
Expect
BDD 風格透過 expect
或 should
介面公開。在這兩種情況下,您都可以將自然語言斷言鏈接在一起。
var expect = require('chai').expect
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(beverages).to.have.property('tea').with.lengthOf(3);
Expect 也允許您包含任意訊息,以便在可能發生的任何失敗斷言之前添加這些訊息。
var answer = 43;
// AssertionError: expected 43 to equal 42.
expect(answer).to.equal(42);
// AssertionError: topic [answer]: expected 43 to equal 42.
expect(answer, 'topic [answer]').to.equal(42);
當與非描述性主題(例如布林值或數字)一起使用時,這會很方便。
Should
should
風格允許與 expect
介面相同的可鏈接斷言,但它使用 should
屬性擴展每個物件以開始您的鏈。當在 Internet Explorer 中使用此風格時,會有一些問題,因此請注意瀏覽器相容性。
var should = require('chai').should() //actually call the function
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
beverages.should.have.property('tea').with.lengthOf(3);
差異
首先,請注意,expect
require 只是對 expect
函數的參考,而 should
require 則正在執行該函數。
var chai = require('chai')
, expect = chai.expect
, should = chai.should();
expect
介面提供了一個函數,作為鏈接您的語言斷言的起點。它適用於 node.js 和所有瀏覽器。
should
介面擴展 Object.prototype
以提供單一 getter 作為您的語言斷言的起點。它適用於 node.js 和除 Internet Explorer 之外的所有現代瀏覽器。
Should 額外功能
由於 should
透過擴展 Object.prototype
來工作,因此在某些情況下 should
無法工作。主要是,如果您嘗試檢查物件是否存在。以下列虛擬程式碼為例
db.get(1234, function (err, doc) {
// we expect error to not exist
// we expect doc to exist and be an object
});
假設 err
應為 null 或 undefined,err.should.not.exist
不是有效的陳述式,因為 undefined
和 null
沒有使用 should
鏈啟動器進行擴展。因此,此情境的幾個適當斷言如下
var should = require('chai').should();
db.get(1234, function (err, doc) {
should.not.exist(err);
should.exist(doc);
doc.should.be.an('object');
});
如果您將 should
指派給一個變數,您可以使用幾個快速輔助函式,以避免在使用 should
時遇到問題。
should.exist
should.not.exist
should.equal
should.not.equal
should.Throw
should.not.Throw
在 ES2015 中使用 Should
無法從 ES2015 import
陳述式鏈接函數調用 – 它必須放在自己的行中,這看起來有點冗長
import chai from 'chai';
chai.should();
為了更簡潔,您可以改為執行此操作
import 'chai/register-should';
設定
config.includeStack
- @param {Boolean}
- @default
false
使用者可設定的屬性,會影響堆疊追蹤是否包含在斷言錯誤訊息中。預設值 false
會在錯誤訊息中隱藏堆疊追蹤。
chai.config.includeStack = true; // turn on stack trace
config.showDiff
- @param {Boolean}
- @default
true
使用者可設定的屬性,會影響是否應將 showDiff
標記包含在擲回的 AssertionErrors 中。false
將始終為 false
;當斷言要求顯示差異時,true
將為 true。
chai.config.showDiff = false; // turn off reporter diff display
config.truncateThreshold
- @param {Number}
- @default
40
使用者可設定的屬性,設定斷言錯誤中實際值和預期值的長度閾值。如果超過此閾值,則會截斷該值。
如果您想完全停用截斷,請將其設定為零。
chai.config.truncateThreshold = 0; // disable truncating