我最近尝试结合 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/