W języku Javascript zanieczyszczanie globalnej przestrzeni nazw jest ogólnie uważane za złe. Właśnie dlatego Coffeescript opakowuje całą twoją JavaScript w opakowaniu (function() {}).call(this);
.Zakres QUnit + coffeescript
Jednak zacząłem pisać testy QUnit dla mojego kodu Coffeescript, a QUnit narzeka, że nie może znaleźć moich funkcji.
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
Chciałbym przetestować zmienne bez zanieczyszczania globalnej przestrzeni nazw. Jaki jest dobry sposób na zrobienie tego?
tutaj jest generowany JavaScript Chcę testu:
(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);
i mój plik test.js jest:
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"));
});
Dzięki, Kevin
Niektóre przykładowy kod byłoby pomocne. –
Cześć Aaron, dodałem przykład. –