assert.pushResult()
バージョン追加: 1.22.0.
説明
pushResult( data )
カスタムアサーションの結果を報告します。
名前 | 説明 |
---|---|
data.result (boolean) |
アサーションの結果 |
data.actual |
テスト対象の式 |
data.expected |
既知の比較対象の値 |
data.message (文字列または未定義) |
アサーションの簡単な説明 |
例
組み込みの QUnit アサーションでは抽象化できない想定を表現する必要がある場合は、Expression で独自の論理をアドホックに実行してから、2 つの直接比較される値を assert.strictEqual()
に渡すか、独自の代表的なブール結果を assert.true()
に渡します。
QUnit.test('bad example of remainder', assert => {
const result = 4;
const actual = (result % 3) === 2;
assert.true(actual, 'remainder');
// In case of failure:
// > Actual: false
// > Expected: true
//
// No mention of the actual remainder.
// No mention of the expected value.
});
QUnit.test('good example of remainder', assert => {
const result = 4;
assert.strictEqual(result % 3, 2, 'remainder');
// In case of failure:
// > Actual: 1
// > Expected: 2
});
QUnit.test('bad example of between', assert => {
const actual = 3;
const isBetween = (actual >= 1 && actual <= 10);
assert.true(isBetween, 'result between 1 and 10');
// In case of failure:
// > Actual: false
// > Expected: true
//
// No mention of the actual remainder.
// No mention of the expected value.
// Cannot be expressed in a useful way with strictEqual()
});
カスタムアサーション
カスタムアサーションメソッドを使用すると、アサーションを評価する方法を、障害が発生した場合に実際の値と想定値をどのように説明するかとは別に制御できます。
たとえば
QUnit.assert.between = function (actual, from, to, message) {
const isBetween = (actual >= from && actual <= to);
this.pushResult({
result: isBetween,
actual: actual,
expected: `between ${from} and ${to} inclusive`,
message: message
});
};
QUnit.test('custom assertion example', assert => {
const result = 3;
assert.between(result, 1, 10, 'result');
// Example of failure if result is out of range
// > actual: 42
// > expected: between 1 and 10
});