单元测试角度指令

时间:2014-09-11 08:42:55

标签: angularjs unit-testing angularjs-directive jasmine karma-runner

我有这样的指示:

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来编写单元测试。 我该怎么做呢。

1 个答案:

答案 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');
   }));
});

希望这有帮助。