我正在使用Karma和Jasmine测试控制器。我的控制器和组件写在两个文件中:
module.component('A', {
templateUrl: 'template.html',
controller: 'YourController'
});
在另一个文件中,我定义了控制器:
function() {
'use strict';
angular.module('app').controller('YourController',
YourController);
YourController.$inject = [ '$scope', '$http', '$window',
'$rootScope' ];
function YourController($scope, $http, $window, $rootScope) {
function yourcontrollerfunction() {
}
yourcontrollerfunction();
}
})();
代码正常工作。但是,当我使用Karma和Jasmine测试控制器时,总是会得到一个错误,指出未定义 YourController 。我不明白为什么。请给我任何提示。谢谢!
describe('test1', function () {
var $componentController;
beforeEach(module('app'));
beforeEach(angular.mock.inject(function(_$componentController_) {
$componentController = _$componentController_;
}));
it('test controller', function () {
var ctrl = $componentController('A',null);
});
}); // end of describe