如何使用 QUnit 和 TypeScript 加载公共函数 [英] How to load a public function using QUnit and TypeScript

查看:61
本文介绍了如何使用 QUnit 和 TypeScript 加载公共函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 QUnit 来测试我的打字稿代码,当我运行这样一个简单的例子时一切都很好: 来做到这一点.

有点谷歌搜索会抛出这个 https://github.com/jrburke/requirejs/wiki/Test-frameworks 可以帮助您让 QUnit 与异步模块一起工作.

以及本次讨论关于将 Chutzpah 与 Require 结合使用,链接到 这个例子.

I am using QUnit to test my typescript code and everything is fine when I run a simple example like this: http://thomasardal.com/testing-typescript-with-typescript-using-qunit-and-chutzpah/

But my nightmare start when I try create unit tests for my SPA app. At the moment to run the testing using Chutzpah on my VS I got a strange error: "Can't Find variable home in mypath\home.tests.ts(line6).

My Code bellow:

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 ");

});

here you are my typeScript settings:

any idea what is wrong with my code?

UPDATE 1 The complete message from output windows in Vs2012 is:

Test 'home.ts tests:test activate function from home viewmodel' failed Died on test #1 at file:///C:/Users/rolando/AppData/Local/Microsoft/VisualStudio/11.0/Extensions/kyo4ap1e.tvo/TestFiles/QUnit/qunit.js:412 at file:///D:/Mercatus/SourceCode/KILN/AquaVet2/SW/AquaVet.Web/App/viewmodels/_Chutzpah.7.home.tests.js:6: Can't find variable: home in D:\Mercatus\SourceCode\KILN\AquaVet2\SW\AquaVet.Web\App\viewmodels\home.tests.ts (line 6)

0 passed, 1 failed, 1 total (chutzpah).

UPDATE 2 As you see in the code i am trying load home.ts using the keyword module("home").... I am not sure if that could be the reason of my problems. A better solution could be add a internal reference to home.ts

but i don't know how I can reference to activate function !!.

解决方案

Just adding an import statement doesn't include an AMD module - you need a loader such as require.js to do that.

A bit of Googling throws up this https://github.com/jrburke/requirejs/wiki/Test-frameworks which may help you get QUnit working with async modules.

And this discussion about using Chutzpah with Require, which links to this example.

这篇关于如何使用 QUnit 和 TypeScript 加载公共函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆