繁体   English   中英

如何使用QUnit和TypeScript加载公共函数

[英]How to load a public function using QUnit and TypeScript

我正在使用QUnit来测试我的打字稿代码,当我运行这样一个简单的例子时,一切都很好: 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测试:从home viewmodel测试激活功能'失败死于测试#1的文件:/// C:/Users/rolando/AppData/Local/Microsoft/VisualStudio/11.0/Extensions/kyo4ap1e.tvo/TestFiles /QUnit/qunit.js:412在file:/// D:/Mercatus/SourceCode/KILN/AquaVet2/SW/AquaVet.Web/App/viewmodels/_Chutzpah.7.home.tests.js:6:不能找到变量:D:\\ Mercatus \\ SourceCode \\ KILN \\ AquaVet2 \\ SW \\ AquaVet.Web \\ App \\ viewmodels \\ home.tests.ts(第6行)

0通过,1失败,1总(chutzpah)。

更新2正如您在代码中看到的那样,我正在尝试使用关键字模块(“home”)加载home.ts ....我不确定这是否是我遇到问题的原因。 更好的解决方案可以是添加home.ts的内部引用

添加home.ts作为内部参考

但我不知道如何引用激活功能!!

只是添加一个import语句不包含AMD模块 - 你需要一个像require.js这样的加载器来做到这一点。

一些谷歌搜索引发了这个https://github.com/jrburke/requirejs/wiki/Test-frameworks ,它可以帮助你让QUnit使用异步模块。

这个讨论关于使用Chutzpah和Require,它链接到这个例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM