我有这样的指示:
angular.module('default', []);
angular.module('default').
directive('default', function()
{
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('error', function() {
angular.element(this).attr("src", attrs.default);
});
}
}
});
我想为这个指令编写单元测试。 我正在使用karma-jasmine来编写单元测试。 我该怎么做呢。
答案 0 :(得分:1)
这将是这样的:
describe('default directive', function () {
it('Should set attribute src to value of attribute default', inject(function ($compile, $rootscope) {
var scope = $rootscope;
var elem = angular.element('<div default="test"></div>');
elem = $compile(elem)(scope);
expect(elem.children(0)[0].getAttribute('src')).toBe('test');
}));
});
希望这有帮助。