我有一个声明为属性的指令:
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');
});
});
如何成功测试声明为Attribute的指令?我正在使用Karma,Jasmine和PhantomJS
答案 0 :(得分:1)
当您拥有ng-transclude
时,您需要在模板中的某个位置transclude: true
,以便angular知道注入HTML的位置。尝试:
app.directive('myDirective', function() {
return {
restrict: 'A',
replace: true,
transclude: true,
scope: {
data: "="
},
template:
'<div ng-transclude><p class="my-paragrapgh">' +
'<label>Hello</label>' +
'</p></div>'
}
});
<强>更新强>
看起来可能导致问题的是replace
选项。
app.directive('myDirective', function() {
return {
restrict: 'A',
scope: {
data: "="
},
template:
'<p class="my-paragrapgh">' +
'<label>Hello</label>' +
'</p>'
}
});
使用replace: true
您的内部HTML是:
失败
<label>Hello</label>
replace
未定义,您有
通
<p class="my-paragrapgh"><label>Hello</label></p>
答案 1 :(得分:0)
即使没有replace
transclude
和ng-trasclude
angular.module('myModule', [])
.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;
// Even we can introduce our Jasmine custom matcher
beforeEach(function() {
jasmine.addMatchers({
toHaveCSSClass: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
debugger;
var result = {};
result.pass = util.equals(actual.hasClass(expected), true, customEqualityTesters);
if (result.pass) {
result.message = "Expected " + actual + " to not have CSS class '" + expected + "'";
} else {
result.message = "Expected " + actual + " to have CSS class '" + expected + "'";
}
return result;
}
}
}
});
});
beforeEach(module('myModule'));
beforeEach(inject(function($rootScope, $compile) {
scope = $rootScope.$new();
element = angular.element("<div my-directive></div>");
$compile(element);
scope.$digest();
}));
it('has a my-paragrapgh class', function() {
expect(element.hasClass('my-paragrapgh')).toBeTruthy();
});
it('has a my-paragrapgh class - tested by custom matcher', function() {
expect(element).toHaveCSSClass('my-paragrapgh')
});
});
<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-mocks.js"></script>