How to load a public function using QUnit and Type

2019-08-12 03:26发布

问题:

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 !!.

回答1:

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.