【问题标题】:Unit testing angular directive单元测试角度指令
【发布时间】:2014-09-11 08:42:54
【问题描述】:

我有这样的指令:

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

【问题讨论】:

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


    【解决方案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');
       }));
    });
    

    希望这会有所帮助。

    【讨论】:

    • 嗨。谢谢@克里斯蒂娜。但是我没有按照expect(elem.children(0)[0].getAttribute('src')).toBe('test');声明
    • 我有另一个指令,其中包含外部 html 模板。如何去测试这样的指令?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2020-11-11
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    相关资源
    最近更新 更多