unit-testing - 使用 Chutzpah 运行 Typescript qUnit 测试

标签 unit-testing visual-studio-2012 typescript qunit chutzpah

我最近尝试结合 qUnit 和 Chutzpah 来对我正在处理的一个用 typescript 编写的项目进行单元测试。

我已经按照我认为正确的方式进行了设置,并且以下内容有效:

  • typescript 应用程序! - 我可以运行应用程序的早期版本
  • Chutzpah - 我在 VS2012 上安装了它,它正确地看到了我的 qUnit 演示测试
  • qUnit - 似乎已安装并与 Chutzpah 一起工作,我可以运行一个简单的测试(一个不查看我的代码的测试)

有了这三个,我想我可以开始为 typescript 应用程序编写测试,作为测试,我用 typescript 编写了一个简单的测试:

TreeBurst.Tests.ts

///<reference path="../typings/qunit/qunit.d.ts" />
///<reference path="references.ts">
///<reference path="references.js"> // just in case, unsure what needed referencing here

module DMC.TreeBurst {

QUnit.module("TreeBurst.Node.ts tests");

test("Load-a-single-node-creates-correctly", () => {

    var node = [new DMC.TreeBurst.Node({
        id: 1,
        parentId: null,
        title: ""
    })];

    ok(node, "Node not created correctly when provided valid constructor parameters");

});
}

不幸的是,当我运行它时,我得到以下信息:

'undefined' is not a constructor (evaluating 'new DMC.TreeBurst.Node({
                id: 1,
                parentId: null,
                title: ""
            })')

现在我很确定它应该是一个构造函数,但是 Chutzpah 和 qUnit 的组合似乎并不这么认为。

我花了一些时间搜索,但我发现的一切都表明事情应该“有效”。我在这里做错了什么吗?

非常感谢任何想法。

编辑:作为对评论的回应,这是类声明:

/// <reference path="references.ts" />
module DMC.TreeBurst {

export interface NodeOptions {

    // location specific
    id: number;
    parentId?: number;

    // node internals
    title: string;
    content?: string;
    colour?: string;        
}

export class Node {

    public id: number;
    public parentId: number = null;
    public title: string;
    public content: string = null;        
    public depth: number = null;

    public colour: string = null;

    constructor(opts: NodeOptions) {
        //removed from brevity
    }
    // methods removed for brevity
}
}

最佳答案

我能够让您的示例使用您的测试文件和代码文件的以下定义。我将所有文件放在同一目录中,但您可以轻松移动它们并更改路径。

TreeBurst.tests.ts

///<reference path="qunit.d.ts" />
///<reference path="TreeBurst.ts" />

module DMC.TreeBurst {

QUnit.module("TreeBurst.Node.ts tests");

test("Load-a-single-node-creates-correctly", () => {

    var node = [new DMC.TreeBurst.Node({
        id: 1,
        parentId: null,
        title: ""
    })];

    ok(node, "Node not created correctly when provided valid constructor parameters");

});
}

TreeBurst.ts

module DMC.TreeBurst {

export interface NodeOptions {

    // location specific
    id: number;
    parentId?: number;

    // node internals
    title: string;
    content?: string;
    colour?: string;        
}

export class Node {

    public id: number;
    public parentId: number = null;
    public title: string;
    public content: string = null;        
    public depth: number = null;

    public colour: string = null;

    constructor(opts: NodeOptions) {
        //removed from brevity
    }
    // methods removed for brevity
}
}

关于unit-testing - 使用 Chutzpah 运行 Typescript qUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19986705/

相关文章:

ios - 如何从单元测试类访问静态变量?

c# - 使用c#读取Excel文件中的行

javascript - 我可以在不编写循环的情况下声明嵌套在对象集合中的值数组吗?

javascript - GraphQL:更新数组

typescript - TypeScript 中带有泛型的条件类型

powershell - 运行pester测试时如何使管道中的输出出现在控制台上?

unit-testing - 在没有 mstest.exe 的情况下运行 VSTS 测试

javascript - 相同的代码,Fiddle 与 VS 中的不同结果

c++ - 让 visual studio 2012 自动复制引用项目的引用 (C++)

c# - 使用 Visual Studio 同时构建 32 位和 64 位