如何增加测试 angular 指令的覆盖率?

How to increase the coverage on testing angular directive?

所以这是我的 angular 指令。使用模板的简单方法 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%?

报告图片如下 http://imgur.com/NRzTjyZ

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

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

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