【发布时间】:2014-04-04 16:05:52
【问题描述】:
我无法成功地对我的 angular.js 指令(karma + jasmine)进行单元测试...
基本上,在我的指令的编译函数中,我确实将元素内容替换为表单。
进行单元测试时,在
scope = $rootScope;
element = angular.element('<my-directive></my-directive>');
$compile(element)($rootScope);
scope.$digest();
我希望找到包含我的表单的元素,带有'... abc ...'...
事实并非如此...... :-(
这是我的(简化的)指令:
angular.module('myApp')
.directive('myDirective', function() {
return {
restrict: 'E',
scope: {},
compile: function(element) {
element.replaceWith('<form> ... abc ... </form>');
}
};
});
这是我的测试:
describe('Directive: paypalButton', function () {
beforeEach(angular.mock.module('myApp'));
var element, scope;
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope;
element = angular.element('<my-directive></my-directive>');
$compile(element)($rootScope);
scope.$digest();
}));
it('replaced content should contain abc', function() {
expect(element.html()).toContain('abc');
});
});
该指令有效(在浏览器中我看到“abc”),但“预期”测试总是失败:我在元素的 html() 中没有得到 'abc',但总是得到 'xyz'...
我确定我遗漏了一些明显的东西...... :-(
【问题讨论】:
标签: angularjs angularjs-directive karma-runner