我目前参与的一个项目,该项目使用打字稿构建其单页 anuglar 应用程序。 不幸的是,他们没有进行单元测试。 作为使用打字稿的新手,我在进行任何测试时遇到了问题。 下面是一个示例测试。后跟打字稿文件
当我运行测试时,它找不到控制器!
(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 控制器(在你的测试中):
CarController = new CarDealerApp.Cars.Controllers.CarController($rootScope.$new());
这是一个更干净的结构,也是编写TypeScript测试时的正确方法。
终于开始工作了。
基本上我需要像这样传入控制器的完整模块名称:
CarController = $controller('CarDealerApp.Cars.Controllers.CarController');