تست واحد - استفاده از Chutzpah برای اجرای تستهای qUnit در تایپاسکریپت
من اخیراً سعی کردهام qUnit و Chutzpah را برای انجام تست واحد روی پروژهای که روی آن کار میکنم و با تایپاسکریپت نوشته شده است، به کار ببرم.
من کارها را به نحوی که فکر میکنم درست است تنظیم کردهام و موارد زیر کار میکنند:
- برنامه تایپاسکریپت! - من میتوانم یک نسخه بسیار اولیه از برنامه را اجرا کنم
- خودخواهی - من این را روی VS2012 نصب کردم و به درستی تست نمایشی qUnit من را میبیند
- qUnit - به نظر میرسد نصب شده و با Chutzpah کار میکند، من میتوانم یک تست ساده اجرا کنم (تستی که به کد من نگاه نمیکند)
با وجود این سه مورد، فرض کردم که میتوانم نوشتن تستها برای اپلیکیشن تایپاسکریپت را آغاز کنم، به عنوان یک تست، یک تست ساده در تایپاسکریپت نوشتم:
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
}
}
پاسخ شماره ۲
اضافه کردن ارجاع به یک فایل js هیچ تأثیری ندارد. همچنین به جای ارجاع دادن به reference.ts، به پوشه http://matthewmanela.com/blog/chutzpah-2-2-with-typescript-support/ ارجاع دهید. این به دلیل نحوه کار چوتزپاه است (فایلهای ارجاع شده را با --out کامپایل نمیکند)
