【发布时间】:2015-10-29 18:19:52
【问题描述】:
我正在努力从用于单元测试的指令中获取控制器。这是我的角度应用程序:
angular.module('test-app', [])
.controller('loadingCtr', ['$scope', function ($scope) {
}])
.directive('loading', function() {
return {
restrict: 'E',
controller: 'loadingCtr'
};
});
这是我的单元测试代码:
describe('loading', function () {
beforeEach(inject(function($rootScope, $compile) {
var fooElement = $compile('<loading></loading>')($rootScope);
var fooController = fooElement.controller('loading');
$rootScope.$digest();
console.log(fooController);
}));
describe('loading: testing loading directive', function() {
it('should create loading directive', function() {
});
});
});
这里有一个 plnkr 可以玩弄:http://plnkr.co/edit/38cc5HQFgeHDhnC8OMo8?p=info
fooController 总是返回未定义。我尝试使用我在网上找到的以下示例,但我总是得到相同的结果:
Unit testing a directive that defines a controller in AngularJS
这里有什么我遗漏的明显东西吗?
【问题讨论】:
标签: angularjs unit-testing angularjs-directive jasmine karma-jasmine