【发布时间】:2013-12-20 17:22:04
【问题描述】:
我想问你如何测试接受另一个类的构造函数实例的类。例如我想测试方法'hasChildRoutes()':
class Route implements ng.route.IRoute {
public url: string;
public config: RouteConfig;
constructor(url: string, config: RouteConfig) {
this.url = url;
this.config = config;
}
public hasChildRoutes(): boolean {
return this.config.childRoutes.length > 0;
}
}
我为此编写了糟糕的单元测试(我创建了另一个类的新实例,这在我看来很糟糕):
beforeEach(() => {
routeSetting = new RouteSetting(1, '');
routeConfig = new RouteConfig('', '', routeSetting, [], '');
});
describe('Methods test', () => {
var childRoute: Route;
beforeEach(() => {
route = new Route('', routeConfig);
});
it('sould return false when Route has no child routes', () => {
expect(route.hasChildRoutes()).toBeFalsy();
});
it('sould return true when Route has child routes', () => {
routeConfig = new RouteConfig('', '', routeSetting, [route], '');
route = new Route('', routeConfig);
expect(route.hasChildRoutes()).toBeTruthy();
});
});
【问题讨论】:
标签: javascript unit-testing typescript jasmine