我在Jasmine中对一些Angular指令进行单元测试,并在PhantomJS无头浏览器中使用Karma运行测试。
我有几百个规格。自从我开始测试指令以来,我注意到PhantomJS在完成整个测试套件几次后占用了大量内存。我有一种预感,这是因为指令(可能还有其他垃圾)在测试后没有被释放。
我确实有一些describe('leftNavigation', function() {
var nlElement = angular.element('<div left-navigation></div>');
beforeEach(module('app'));
beforeEach(module('path/to/leftNavigation.html'));
beforeEach(inject(function($templateCache, _$compile_, _$rootScope_) {
template = $templateCache.get('full/path/to/leftNavigation.html');
$templateCache.put('full/path/to/leftNavigation.html', template);
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('should contain the text BLAH', function() {
var element = $compile(nlElement)($rootScope);
$rootScope.$digest();
expect(element.text()).toContain('BLAH');
});
// Teardown
afterEach(function() {
(nlElement).remove();
});
});
语句在使用后删除了“编译”指令,但似乎这还不够,或者我没有正确地执行它。
example.spec.js
nlElement
我是否正在拆除正确的变量(element
),还是需要拆除afterEach
?每个beforeEach
语句都需要'use strict';
// Grab ref to app module
var app = angular.module('app');
app.directive('leftNavigation', ['$log',
function ($log) {
return {
restrict: 'A',
replace: true,
templateUrl: 'path/to/template.html',
scope:{
selection: '@'
}
};
}
]); //end of directive
个语句吗?
编辑:指令定义如下:
ng-class
编辑2:该指令的HTML模板在多个元素上包含AngularJS表达式和{{1}}属性。