我目前正在参与一个项目,该项目使用 typescript 构建其单页 anuglar 应用程序。不幸的是他们没有进行单元测试。作为使用 typescript 的新手,我在进行任何测试时遇到问题。下面是一个测试示例。接下来是 typescript 文件
当我运行测试时,它找不到 Controller !
(function() {
'use strict';
describe('CarController', function() {
// Load the controllers module
beforeEach(module('CarDealerApp'));
var scope,
CarController ;
beforeEach(inject(function($controller) {
scope = {};
CarController = $controller('CarController', {
$scope: scope
});
}));
it('should set the car name', function() {
expect(scope.carMake).toEqual('Ford');
});
});
})();
module CarDealerApp.Cars.Controllers {
"use strict";
export interface ICarScope extends angular.Scope {
carName : string;
}
export class CarController {
carName:string = "Ford";
// $inject annotation
public static $inject = [
'$scope'
]
constructor(private $scope: ICarScope){
$scope.vm = this;
}
}
}
最佳答案
尽管您在那里编写的内容有效,但我建议像这样构造您的 TypeScript Controller (在您的测试中):
CarController = new CarDealerApp.Cars.Controllers.CarController($rootScope.$new());
这是一种更简洁的结构,并且是您编写 TypeScript 测试时的正确方法。
关于angularjs - Angular JS Typescript 单元测试 Karma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20910768/