【问题标题】:How to increase the coverage on testing angular directive?如何增加测试角度指令的覆盖率?
【发布时间】:2015-05-21 19:36:03
【问题描述】:

所以这是我的角度指令。使用模板 url 的简单方法

  angular.module('my.directives')
    .directive('userNameDisplay', function() {
      return {
        restrict: 'E',
        scope: {
          user: '=user',
          status: '=status'
        },
        templateUrl: '/partials/userNameDisplay.html'
      };
    });

规格如下。它再次尝试涵盖所有情况。

describe('user-name-display', function () {
  var elm, scope;

  beforeEach(module('my.directives', '/partials/userNameDisplay.html'));

  beforeEach(inject(function ($compile, $rootScope) {
    scope = $rootScope;
    elm = angular.element('<user-name-display user="someUser" status="status"></user-name-display>');
    $compile(elm)(scope);
  }));

  it('should have the correct isolate scope values', function () {
    scope.someUser = {
      name: "John",
      color: "blue"
    };
    scope.status = true;
    scope.$digest();

    var isoScope = elm.isolateScope();
    expect(isoScope.user.name).toBe('John');
    expect(isoScope.displayCollaboratorStatus).toBe(true);
  });

  it('should render html within the partial accordingly', function () {
    scope.someUser = {
      name: "John"
    };
    scope.status = false;
    scope.$digest();

    var cBoxSpan = elm.find("span.user-collaborator-box");
    expect(cBoxSpan.length).toBe(0);

    var userNameBox = elm.find("span.user-name");
    expect(userNameBox[0].innerHTML).toBe("John");
  });
});

覆盖率报告如下所示。我正在使用 Karma(使用伊斯坦布尔)来获取代码覆盖率。我正在尝试将其增加到 100%。我无法从报告中弄清楚我错过了什么。它说 return 语句从未被命中,但没有它,隔离绑定将不会发生。如何让覆盖率达到 100%?

Here is the image of the report http://imgur.com/NRzTjyZ

【问题讨论】:

    标签: angularjs code-coverage karma-runner istanbul


    【解决方案1】:

    我认为您不会从 beforeEach 块中获得覆盖。

    尝试添加此测试(它与您的 beforeEach 代码相同):

        it('should compile', function() {
            scope = $rootScope;
            elm = angular.element('<user-name-display user="someUser" status="status"></user-name-display>');
            $compile(elm)(scope);
        });
    

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多