Javascript 无法在测试指令中使用$rootScope.$new()
我的指令有一个单元测试:Javascript 无法在测试指令中使用$rootScope.$new(),javascript,unit-testing,angularjs,angularjs-directive,Javascript,Unit Testing,Angularjs,Angularjs Directive,我的指令有一个单元测试: describe('Directive: ov-tabs', function() { var $compile, scope,subScope, element, template, doc,timeout,window; beforeEach(module('ngnms.ui.tabs','layout.html')); beforeEach(inject(function(_$compile_, $rootScope,$templateCache, $
describe('Directive: ov-tabs', function() {
var $compile, scope,subScope, element, template, doc,timeout,window;
beforeEach(module('ngnms.ui.tabs','layout.html'));
beforeEach(inject(function(_$compile_, $rootScope,$templateCache, $timeout,$document, $window) {
template = $templateCache.get('layout.html');
$templateCache.put('template/tab-layout.html', template);
$compile = _$compile_;
timeout = $timeout;
window = angular.element($window);
doc = $document;
scope = $rootScope.$new();
var html =
'<ov-tabset ov-tabs="items" on-tab-selected="tabSelectCallback" on-tab-closed="tabClosedCallback"></ov-tabset>';
element = angular.element(html);
$(element).appendTo($('body'));
//------------------------------------------init scope
//add 12 items to scope
//-------------------------------------------end init scope
$compile(element)(scope);
scope.$digest();
}));
it('some thing true',function(){});
var lis = element.find('li');
expect(lis.length).toEqual(12);
})
descripe('Directive:ov tabs',function(){
var$compile、作用域、子作用域、元素、模板、文档、超时、窗口;
在每个模块之前(模块('ngnms.ui.tabs','layout.html');
beforeach(注入函数($compile、$rootScope、$templateCache、$timeout、$document、$window){
template=$templateCache.get('layout.html');
$templateCache.put('template/tab layout.html',template);
$compile=\$compile;
超时=$timeout;
window=angular.element($window);
doc=$document;
scope=$rootScope.$new();
变量html=
'';
element=angular.element(html);
$(元素).appendTo($('body');
//------------------------------------------初始范围
//将12项添加到范围
//-------------------------------------------终止初始范围
$compile(元素)(范围);
范围。$digest();
}));
它('something true',function(){});
var lis=element.find('li');
期望(lis.长度)、toEqual(12);
})
与DOM交互的结果抛出错误
如果我将“scope=$rootScope.$new();”改为“scope=$rootScope;”,它们会工作得很好
我希望测试sope到$destroy()
。因为$rootScope
无法调用$destroy()
错误记录在这里!
我创建的每个预期都会抛出错误!但如果我改为rootScope,它做得很好。我认为它无法编译DOM!?可能是严重的,是什么错误我上传了一张图片!