我正在使用Smart Table并尝试使用Karma设置一些测试。我一再得到错误Error: [$compile:ctreq] Controller 'stTable', required by directive 'removePag', can't be found!
。实际使用该指令时,我不会收到任何错误。在我看来,Karma应该让智能表可用于tools
模块,这是我在规范中提到的,但没有骰子。我如何确定stTable
可用于removePag
的测试?如何确保控制器注入指令进行测试?
在tools.js(包括expose
因为我怀疑它可能参与,因为它是连接到顶层st-table
一个指令):
angular.module('tools', ['smart-table']);
angular
.module('freeTools')
.directive('expose', exposeTableState)
.directive('removePag', removePag)
function exposeTableState(){
return {
require:'stTable',
link:function(scope, element, attr, ctrl){
scope.smartTableState=ctrl.tableState();
}
};
}
function removePag() {
return {
restrict: 'E',
require: '^stTable',
template: '<a href="">View as a single page</a>',
link: function(scope, element, attrs, ctrl) {
return element.bind('click', function() {
return scope.$apply(function() {
var tableState;
tableState = ctrl.tableState();
tableState.pagination.number = tableState.pagination.totalItemCount;
return ctrl.pipe();
});
});
}
};
}
在toolsSpec.js:
describe('tools', function() {
var $compile,
$rootScope;
beforeEach(module('freeTools'));
beforeEach(inject(function(_$compile_, _$rootScope_){
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('Replaces the element with the appropriate content', function() {
var element = $compile("<remove-pag></remove-pag>")($rootScope);
$rootScope.$digest();
expect(element.html()).toContain("View as a single page");
});
}
在Karma.conf:
module.exports = function(config){
config.set({
basePath : '../../',
files : [
'bower_components/jquery/dist/jquery.js',
'bower_components/angular/angular.js',
'bower_components/angular-smart-table/dist/smart-table.js',
'bower_components/angular-mocks/angular-mocks.js',
'assets/scripts/*.js',
'assets/test/*.js'
],
autoWatch : true,
frameworks: ['jasmine-ajax', 'jasmine'],
browsers : ['Chrome'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine-ajax',
'karma-jasmine',
],
});
};
简体HTML:
<section ng-app="tools">
<table st-table="data" st-safe-src="safe" expose>
<div st-pagination>
<remove-pag></remove-pag>
我意识到这个问题是非常相似的Unit testing an AngularJS (Smart Table) directive。正如那里所暗示的那样,模拟智能表测试自己的控制器的方式也不起作用。无论如何,模块的源代码中的stTable
似乎是一个指令,而不是控制器。
我也试过在测试中用st表包装元素,就像$compile("<table st-table><remove-pag>...
有相同的错误。
我查看了像Controller Required By Directive Can't Be Found这样的问题,但我已有一个共享的父项tools
。