我正在使用 QUnit 来测试我的 typescript 代码,当我运行一个像这样的简单示例时一切正常:http://thomasardal.com/testing-typescript-with-typescript-using-qunit-and-chutzpah/
但是当我尝试为我的 SPA 应用程序创建单元测试时,我的噩梦开始了。 在我的 VS 上使用 Chutzpah 运行测试时,我遇到了一个奇怪的错误: “无法在 mypath\home.tests.ts(line6) 中找到变量 home。
我的代码如下:
home.ts
import logger = module('services/logger');
export var title = 'Home View';
export function activate() {
logger.log('Home View Activated', null, 'home', true);
return true;
}
home.tests.ts
/// <reference path="../../Scripts/qunit.d.ts" />
QUnit.module("home.ts tests");
import home = module("home");
test("test title from home viewmodel", function () {
// Calling to active public function from home viewmodel. (home.ts)
var activateResult:string = home.title;
// Assert
equal(activateResult, "Home View", "Result should be Home View ");
});
这是我的 typescript 设置:
知道我的代码有什么问题吗?
更新 1 Vs2012 中输出窗口的完整消息是:
测试“home.ts tests:test activate function from home viewmodel”失败 在 file:///C:/Users/rolando/AppData/Local/Microsoft/VisualStudio/11.0/Extensions/kyo4ap1e.tvo/TestFiles/QUnit/qunit.js:412 测试 #1 时死亡 在 file:///D:/Mercatus/SourceCode/KILN/AquaVet2/SW/AquaVet.Web/App/viewmodels/_Chutzpah.7.home.tests.js:6: 找不到变量:home 在 D:\Mercatus\SourceCode\KILN\AquaVet2\SW\AquaVet.Web\App\viewmodels\home.tests.ts(第 6 行)
0 次通过,1 次失败,总计 1 次(放肆)。
更新 2 正如您在代码中看到的,我正在尝试使用关键字 module("home") 加载 home.ts...。我不确定这是否是我出现问题的原因。更好的解决方案可能是添加对 home.ts 的内部引用
但我不知道如何引用激活函数!!
最佳答案
仅添加导入语句不包含 AMD 模块 - 您需要一个加载程序,例如 require.js这样做。
谷歌搜索会抛出这个 https://github.com/jrburke/requirejs/wiki/Test-frameworks这可能会帮助您让 QUnit 使用异步模块。
和this discussion关于将 Chutzpah 与 Require 结合使用,它链接到 this example .
关于javascript - 如何使用 QUnit 和 TypeScript 加载公共(public)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16464984/