グローバルな名前空間を汚染するJavaScriptは一般的に悪いものとみなされます。これは、CoffeescriptがあなたのJavascriptをすべて(function() {}).call(this);
ラッパーにラップする理由です。QUnit + coffeescriptの範囲
しかし、私はCoffeescriptコードのためにQUnitテストを書いています。そして、QUnitは自分の機能を見つけることができないと言っています。
1. Died on test #1: getGoodNamePart is not defined
getGoodNamePart is not defined at Object.<anonymous> (file:///Users/kevin/Documents/docs/code/chrome/tests.js:2:10) at Object.run
グローバル名前空間を汚染することなく変数をテストしたいと思います。これを行うには良い方法は何ですか?
(function() {
getGoodNamePart = function(str) {
if (str.charAt(0) === '"') {
str.replace(/" <[^>]+>$"/g, "");
str.replace(/"/g, "");
return str;
} else if (str.charAt(0) === '<') {
str.replace(/<|>/g, "");
return str;
} else {
return str;
}
};
}).call(this);
と私のtest.jsファイルは次のとおりです:ここで
がテストに私が欲しい生成Javascriptのだそう
test('getGoodNamePart()', function() {
equals(getGoodNamePart("\"Kev Burke\" <[email protected]>"), "Kev Burke", "\"name\" <email> works");
equals(getGoodNamePart("", "", "empty string works"));
equals(getGoodNamePart("[email protected]", "[email protected]", "raw email works"));
return equals(getGoodNamePart("<[email protected]>", "[email protected]", "email inside carets -> carets get stripped"));
});
おかげで、 ケビン
をモジュール化しない
$ coffee -c -b your_file
テスト目的のために、--bareフラグとコーヒーファイルをコンパイルします。 –
こんにちはアーロン、私は例を追加しました。 –