- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Javascript 中,污染全局命名空间通常被认为是一件坏事。这就是 Coffeescript 将你所有的 Javascript 封装在 (function() {}).call(this);
中的原因。包装。
但是,我已经开始为我的 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('getGoodNamePart()', function() {
equals(getGoodNamePart("\"Kev Burke\" <kev@inburke.com>"), "Kev Burke", "\"name\" <email> works");
equals(getGoodNamePart("", "", "empty string works"));
equals(getGoodNamePart("kev@inburke.com", "kev@inburke.com", "raw email works"));
return equals(getGoodNamePart("<kev@inburke.com>", "kev@inburke.com", "email inside carets -> carets get stripped"));
});
最佳答案
所以,你说你想测试getGoodNamePart
不污染全局命名空间。但是,CoffeeScript 自动模块化每个文件(有充分的理由——参见 my answer here),这意味着跨文件访问函数的唯一方法是将它们附加到某个全局对象。 (我假设我们在这里讨论的是浏览器,而不是 CommonJS 环境,例如 Node.js,您将在其中使用 exports
。)
这为您提供了三个选项:
getGoodNamePart
至window
.这是最简单的,因为唯一需要的更改是前缀 getGoodNamePart
与 window.
(或只是 @
),但这当然会最大化命名空间污染。 getGoodNamePart
到已经附加到 window
的其他东西或 global
. getGoodNamePart
相同的文件中(在 JS 世界中是一种不寻常的做法,但值得考虑,因为它保持全局命名空间不变,让您可以轻松地在代码和测试之间切换)。 getGoodNamePart
纯粹为了测试。称它们为“测试目标”。在每个带有测试目标的文件的顶部,添加
window.testTargets ?= {}
getGoodNamePart
, 写
testTargets.getGoodNamePart = getGoodNamePart = (str) ->
...
{getGoodNamePart} = testTargets
关于coffeescript - QUnit + CoffeeScript 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6474703/
我正在尝试将 QUnit 与 Meteor 应用程序一起使用。这应该可能吗?有什么推荐的款式吗? 我试图通过为“/test”创建路由来制作一个“自我测试”的应用程序,但似乎 QUnit 没有运行我的测
我记得曾经在 QUnit 的测试运行器工具栏中看到过 QUnit 测试模块选择列表的屏幕截图。我的印象是,在选择列表中选择一个模块会导致该模块的测试运行。 问题:QUnit 是否真的存在这样的功能 O
是否有任何示例说明如何将 Qunit 实现到完整的开发周期中。现有示例似乎需要将测试脚本硬编码到生产源代码中。目前有没有办法将单元测试和源代码分开?我只想要开发代码中的单元测试代码,而不是生产代码。
此页面上的信息似乎不太可能出现——https://github.com/kof/node-qunit .我有一个安装 nodejs 并安装了 node-quit 模块的设置。我有测试运行器并执行命令
我刚找到 qHint ,一种将 jsHint 测试集成到 Qunit 中的方法......但它在本地(我不是指本地主机)中不起作用,除了在 Firefox 中。 所以我想添加一个“警告”或“通知”,而
我正在研究用于 JavaScript 单元测试的 QUnit。我处于一种奇怪的情况,我正在检查从 Ajax 调用返回的值。 对于下面的测试,我是故意让它不及格的。 // test to check i
js文件 window.onload = function() { document.getElementById('example').addEventListener('mousedown
我当前的单元测试使用 QUnit并且它们按照 QUnit website 上的描述执行.基本上我所有的测试都编译成 tests.js这包含在 index.html 中: QUnit E
我已经想出如何使用 karma 测试运行器测试我的代码,但我不知道如何在网页上测试 UI 功能。 我有一个简单的计算器程序 (calculator.js): window.onload = funct
我刚刚开始使用 QUnit 进行 ui 测试,所以我确定我缺少 qunit-fixture 的一些基本用例。我认为它对测试 DOM 操作很有用,但后来我意识到我的 DOM 操作函数都不知道任何关于 q
我正在通过 Qunit 框架进行 javaScript 单元测试。我有 DOM 对象,其中包含表单和文本框。 我会将上面的 DOM 对象添加到 qunit-f
我有两个 XXXTest.html 文件,每个都与此类似: Somet
我想为 QUnit 编写自定义 assert 函数来检查 actual 字符串是否与 expected 正则表达式匹配。在this question的帮助下我编写了第一个按预期工作的基本版本: QUn
我正在使用QUnit在麻省理工学院许可的项目部分用 TypeScript 编写。我有一些 TS 函数接受 QUnit 作为参数并希望将它们键入作为其接口(interface)来自the typing
最初由 Andreas Haller 在邮件列表上发布,在此处重新发布,以便“qunit-bdd”标签可供其他人使用。 ember-qunit adds a handy moduleFor helpe
使用 instructions here ,我正在尝试通过 chutzpah 配置 QUnit 测试 错误是: Error: Error: Error: Called start() outside
我在为使用 Twitter Bootstrap 的项目编写 qUnit 测试时遇到了困难。当生成模态时,它将覆盖层放在 qunit-fixture 之外,因此当运行下一个测试时,覆盖层不会被删除。有人
这是我的测试代码。 test("user login", function(){ visit("/sessions/new").then(function() { fillIn('inpu
我有一个问题,当我尝试在 div id="qunit-fixture"中附加或设置 HTML 代码时,使用在 ReSharper 8 下运行的 Qunit 进行测试。div id="qunit-fix
抱歉,如果这很明显,但是如果我们想断言某个方法返回 false,QUnit 中是否有 notOK 或等效函数? 我看不到在 documentation 中否定 OK 的方法. 我试过: !ok...
我是一名优秀的程序员,十分优秀!