ng-describe/Angular/Karma 单元测试:使用 ng-describe 的 setupControllers 编译带有所需控制器的指令
ng-describe/Angular/Karma unit testing: Compiling a directive with a required controller using ng-describe's setupControllers
我正在使用 ng-describe
为 angularJS 编写 Karma 单元测试(我必须说,这太棒了)。
我有两个指令。我想测试需要 somethingController
的指令 testing
。
code:
angular.module('A', [])
.directive('testing', function($compile){
return {
restrict: 'E',
template: '<div>THIS TEST</div>',
replace:true,
controllerAs: 'testA',
bindToController: true,
require: '^something',
link: function(){
console.log('this is controller');
}
};
})
.directive('something', function(){
return {
restrict: 'E',
template: '<div id="div1">THIS IS PARENT</div>',
replace:true,
controllerAs: 'sth',
bindToController: true,
controller: function($scope,$compile){
console.log('this is something');
angular.element(document.getElementById('div1'))
.prepend($compile('<div>THIS IS compile</div>')($scope));
}
};
});
如果我使用手动编译,我能够运行这些测试成功。
ngDescribe({
name:'a',
modules: 'A',
inject:['$compile'],
exposeApi:true,
only:true,
tests: function(deps, describeApi){
it('asdfasdf', function(){
var mockModalCtrl = {};
deps.element = angular.element('<testing></testing>');
deps.element.data('$somethingController', mockModalCtrl);
inject(function($compile, $rootScope){
$compile(deps.element)($rootScope.$new());
});
deps.step();
});
}
});
如果我不进行手动编译,我会得到一个错误:
Error: [$compile:ctreq] Controller 'something', required by directive 'testing', can't be found
我真正想做的是:
我正在尝试充分利用 ng-describe 并摆脱手动编译。我试过使用 https://github.com/kensho/ng-describe#secondary-options 中的 describeApi.setupControllers
但我没有运气。
- 使用
describeApi.setupElement('<testing></testing>')
给我留下上面显示的 $compile 错误。
- 使用
describeApi.setupControllers('something')
给我留下错误:Error: [ng:areq] Argument 'something' is not a function, got undefined
- 我尝试设置名为
something
、somethingController
、$something
、$somethingController
的控制器,但我在那里也看到了 undefined, not a function
错误.
有什么方法可以让我完成这项工作吗?到目前为止我无法在网上找到解决方案...
好的。显然,在 SO 上输入一个问题会让你的大脑比平时思考两倍。我找到了解决方案:
var mockModalCtrl = {};
deps.element = angular.element('<testing></testing>');
deps.element.data('$somethingController', mockModalCtrl);
describeApi.setupElement(deps.element);
我正在使用 ng-describe
为 angularJS 编写 Karma 单元测试(我必须说,这太棒了)。
我有两个指令。我想测试需要 somethingController
的指令 testing
。
code:
angular.module('A', [])
.directive('testing', function($compile){
return {
restrict: 'E',
template: '<div>THIS TEST</div>',
replace:true,
controllerAs: 'testA',
bindToController: true,
require: '^something',
link: function(){
console.log('this is controller');
}
};
})
.directive('something', function(){
return {
restrict: 'E',
template: '<div id="div1">THIS IS PARENT</div>',
replace:true,
controllerAs: 'sth',
bindToController: true,
controller: function($scope,$compile){
console.log('this is something');
angular.element(document.getElementById('div1'))
.prepend($compile('<div>THIS IS compile</div>')($scope));
}
};
});
如果我使用手动编译,我能够运行这些测试成功。
ngDescribe({
name:'a',
modules: 'A',
inject:['$compile'],
exposeApi:true,
only:true,
tests: function(deps, describeApi){
it('asdfasdf', function(){
var mockModalCtrl = {};
deps.element = angular.element('<testing></testing>');
deps.element.data('$somethingController', mockModalCtrl);
inject(function($compile, $rootScope){
$compile(deps.element)($rootScope.$new());
});
deps.step();
});
}
});
如果我不进行手动编译,我会得到一个错误:
Error: [$compile:ctreq] Controller 'something', required by directive 'testing', can't be found
我真正想做的是:
我正在尝试充分利用 ng-describe 并摆脱手动编译。我试过使用 https://github.com/kensho/ng-describe#secondary-options 中的 describeApi.setupControllers
但我没有运气。
- 使用
describeApi.setupElement('<testing></testing>')
给我留下上面显示的 $compile 错误。 - 使用
describeApi.setupControllers('something')
给我留下错误:Error: [ng:areq] Argument 'something' is not a function, got undefined
- 我尝试设置名为
something
、somethingController
、$something
、$somethingController
的控制器,但我在那里也看到了undefined, not a function
错误.
有什么方法可以让我完成这项工作吗?到目前为止我无法在网上找到解决方案...
好的。显然,在 SO 上输入一个问题会让你的大脑比平时思考两倍。我找到了解决方案:
var mockModalCtrl = {};
deps.element = angular.element('<testing></testing>');
deps.element.data('$somethingController', mockModalCtrl);
describeApi.setupElement(deps.element);