【发布时间】:2015-10-14 22:59:46
【问题描述】:
我有一个声明为属性的指令:
app.directive('myDirective', function() {
return {
restrict: 'A',
replace: true,
transclude: true,
scope: {
data: "="
},
template:
'<p class="my-paragrapgh">' +
'<label>Hello</label>' +
'</p>'
}
});
我有一个单元测试失败了:
describe('myDirective test', function () {
var scope, compile, element;
beforeEach(module('myModule'));
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
element = angular.element("<div my-directive></div>");
$compile(element);
scope.$digest();
}));
it('should have a my-paragrapgh class', function () {
expect($(element).find('p')[0]).toHaveClass('my-paragrapgh');
});
});
但是,如果我将我的指令转换为元素,并删除替换和转入:
app.directive('myDirective', function() {
return {
restrict: 'E',
//replace: true,
//transclude: true,
scope: {
data: "="
},
template:
'<p class="my-paragrapgh">' +
'<label>Hello</label>' +
'</p>'
}
});
我的单元测试通过了:
describe('myDirective test', function () {
var scope, compile, element;
beforeEach(module('myModule'));
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
element = angular.element("<my-directive></my-directive>");
$compile(element);
scope.$digest();
}));
it('should have a my-paragrapgh class', function () {
expect($(element).find('p')[0]).toHaveClass('my-paragrapgh');
});
});
如何成功测试声明为属性的指令?我正在使用 Karma、Jasmine 和 PhantomJS
【问题讨论】:
标签: angularjs jasmine phantomjs karma-jasmine angular-directive