新しいアサーションをQUnitに追加したいと思います。 私はこれを何かしました:
QUnit.extend(QUnit.assert, {
increases: function(measure, block, message){
var before = measure();
block();
var after = measure();
var passes = before < after;
QUnit.push(passes, after, "< " + before, message);
}
});
私が使うとき increases(foo,bar,baz)
私のテストでは、私は得る
ReferenceError:増加は定義されていません
ブラウザコンソールから私は見ることができます increases
〜にある QUnit.assert
他のすべての標準機能とともに: ok
, equal
, deepEqual
等
コンソールから、次を実行します:
test("foo", function(){console.log(ok) });
のソースを見る ok
.
ランニング:
test("foo", function(){console.log(increases) });
増加は定義されていないと言われています。
テストで私の増加を使用するために必要な魔法は何ですか?また、そのドキュメントはどこに(もしあれば)ありますか?
ありがとう
回答:
回答№1は5解決策は、テストコールバック関数のパラメーターを受け入れることです。そのパラメーターには追加のアサーションタイプがあります。したがって、次のように呼び出すことができます。
//the assert parameter accepted by the callback will contain the "increases" assertion
test("adding 1 increases a number", function(assert){
var number = 42;
function measure(){return number;}
function block(){number += 1;}
assert.increases(measure, block);
});
回答№2については2
今日、カスタムアサーションを追加しようとしましたが、同じ問題が発生しました。 元のアサーション関数のみがグローバルオブジェクトでも定義されています。カスタムアサーションはそうではありません。
QUnitコードのデバッグから、それはオリジナルのようですアサーション関数は、グローバルスコープ(ウィンドウ変数)に意図的に配置されます。これはQUnitの初期化時に発生するため、その時点ですでに定義されている元のアサーション関数にのみ適用されます。
1.QUnit.js:元のアサーション関数の定義
assert = QUnit.assert = {
ok: function( result, msg ) {
...
2.QUnit.js:元のアサーション関数-> QUnit.constructor.prototype
extend( QUnit.constructor.prototype, assert );
3. QUnit.js:QUnit.constructor.prototype->ウィンドウ
// For browser, export only select globals
if ( typeof window !== "undefined" ) {
extend( window, QUnit.constructor.prototype );
window.QUnit = QUnit;
}
溶液
あなたが答えたように、カスタムアサーション関数を使用するには、次のことが必要です。
キャッチ
assert
各アサート関数に渡される引数を使用します。 例test("test name", function(assert) { assert.cosutomAssertion(..); ...});
または、完全な名前空間を使用してassert関数に到達します。 例
QUnit.assert.customAssertion(..)