使用 Chutzpah 运行 Typescript qUnit 测试 [英] Using Chutzpah to run Typescript qUnit tests

查看:47
本文介绍了使用 Chutzpah 运行 Typescript qUnit 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've recently tried to incorporate qUnit and Chutzpah to unit test a project I'm working on which is written in typescript.

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

I've got things setup in what I believe is the correct manner and the following things work:

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

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

With these three in place I presumed I could begin to write tests for the typescript app, as a test I wrote a simple test in 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");

});
}

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

Unfortunately when I run this I get the following:

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

现在我很确定它应该是一个构造函数,但是 Chutzpah 和 qUnit 的组合似乎并没有看到它.

Now I'm pretty sure it SHOULD be a constructor, but the combination of Chutzpah and qUnit doesn't seem to see it at such.

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

I've spent some time searching about, but everything I've found suggests that things should just 'work'. Am I doing something obviously wrong here?

非常感谢任何想法.

为了回应评论,这是类声明:

In response to the comment, this is the class declaration:

/// <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
}
}

推荐答案

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

I was able to get your example working with the following definitions for your test file and code file. I placed all files in the same directory but you can easily move them and change the paths.

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
}
}

这篇关于使用 Chutzpah 运行 Typescript qUnit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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