AngularJS / Karma function - unit test in directive

How can I unit test use a function inside a directory link? I tried something like this but didn't work:

directive:

app.directive("hello", function(){
  return {
    link: function(scope){
      scope.hello = function(){
        return "hello";
      };
    }
  };
});

unit test:

describe("Hello directive", function(){
  var compile, scope;

  beforeEach(inject(function($compile, $rootScope){
    scope = $rootScope.$new();
    compile = $compile;
  }));

  it("should return hello", function(){
    var element = compile("<hello></hello>")(scope);
    expect(element.scope.hello()).toBe("hello");
  });
});
+4
source share
1 answer

You do not need a call module.

The directive constraints look just fine (for now) by default 'EA'according to the angular source for $ compileProvider .

.. angular -1.3.0 (commit: 11f5ae, PR: 8321), 'A'. , , '14.

, ;

  • module, .
  • angular 1.3.0.

jsFiddle


, , [angularjs] + [unit-testing]. , .

+2

Source: https://habr.com/ru/post/1541747/


All Articles