How to have a mock controller pass data to a directive with Jasmine

Phil

I am trying to unit test a directive with Jasmine:

app.directive('helloWorld', function() {
  return {
    restrict: 'E',
    scope: {
      messages: '=',
      onSelect: '&'
    },
    template: '<a ng-repeat="message in messages track by $index" href="" ng-click="onSelect({message: message})">Test: {{ message.name }}</a>',
    link: function(scope) {
    }
  }
});

Ideally, I would like to pass in some mock data for messages, and ensure that the DOM element in the directive is correctly populated.

But I cannot understand how to get the template to compile with the mock data. Specifically, messages: '&' means that the directive is watching whenever the controller updates messages. Here is the test:

describe('Directive: HelloWorld', function() {
    var $scope, ctrl, $compile, $rootScope;

    beforeEach(module('app'));

    beforeEach(inject(function(_$rootScope_, $controller, _$compile_) {
      $rootScope = _$rootScope_;
      $compile = _$compile_;
      $scope = $rootScope.$new();

      ctrl = $controller('MainCtrl', {
        $scope: $scope
      });
    }));

    it('should list three messages', function() {
      ctrl.messages = [{name: 'foo'}, {name: 'bar'}, {name: 'foobar'}];
      var template = '<hello-world messages="messages" on-select="ctrl.foo(message)"></hello-world>';
      var element = $compile(template)($scope);
      $rootScope.$digest();

      expect(element.children().length).toBe(3);
    });
  });

Here is the plunker: https://plnkr.co/edit/8Jy4eGXotduGvBsr8jkN?p=preview

I have looked at many SO questions, but I cannot yet make sense of it. How can I pass on data to the mock controller so that the template has access to it?

Update I modified my Plunker to do this: $scope.messages = [{name: 'foo'}, {name: 'bar'}, {name: 'foobar'}]; instead of ctrl.messages = .... It works fine now.

Brian Campbell

You should be using $scope in your controller and exposing variables that you want to use in your templates by adding them to $scope instead of this.

See this plunk. The relevant changes to make the test pass are in MainCtrl code and line 21 of your test file. Also look at line 18 on index.html to see how to pass the scope variables in html.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I inject a mock dependency into an angular directive with Jasmine on Karma

From Dev

How to mock the Controller of an AngularJS Directive

From Dev

How to pass async data from directive to controller?

From Dev

Call method on Directive to pass data to Controller

From Dev

AngularJS: pass data to directive with existing controller

From Dev

Jasmine Test: How to mock a method inside a controller in AngularJS

From Dev

How to test a directive's controller using angularJS-karma-jasmine?

From Dev

How can I have shared scope and pass data in angular directive

From Dev

How to get Angular Input Directive Data to Pass to Controller

From Dev

How to use/sync data of controller in a directive?

From Dev

how to pass data from controller to directive using $emit

From Dev

How Do I call and pass data to a Directive from a Controller Angular

From Dev

How to mock jQuery with Jasmine

From Dev

How to pass array of data to controller?

From Dev

How to Pass data from directive template to controller?

From Dev

angular + jasmine + mock $stateParams in a directive

From Dev

Pass ajax data from Controller to directive

From Dev

Angularjs how to pass data from a controller to a directive

From Dev

How do I inject a mock dependency into an angular directive with Jasmine on Karma

From Dev

Call method on Directive to pass data to Controller

From Dev

AngularJS: pass data to directive with existing controller

From Dev

angularjs: What is the preferred way to pass data from controller to directive(s)?

From Dev

Jasmine Test: How to mock a method inside a controller in AngularJS

From Dev

How to pass variable $data to controller?

From Dev

How to pass scope value to the directive controller

From Dev

How to pass data from directive to controller?

From Dev

How to pass data from controller to directive from ajax call AngularJS

From Dev

How to pass a method to a directive and invoke it to pass data to a nested directive?

From Dev

How to have a mock controller pass data to a directive with Jasmine

Related Related

  1. 1

    How do I inject a mock dependency into an angular directive with Jasmine on Karma

  2. 2

    How to mock the Controller of an AngularJS Directive

  3. 3

    How to pass async data from directive to controller?

  4. 4

    Call method on Directive to pass data to Controller

  5. 5

    AngularJS: pass data to directive with existing controller

  6. 6

    Jasmine Test: How to mock a method inside a controller in AngularJS

  7. 7

    How to test a directive's controller using angularJS-karma-jasmine?

  8. 8

    How can I have shared scope and pass data in angular directive

  9. 9

    How to get Angular Input Directive Data to Pass to Controller

  10. 10

    How to use/sync data of controller in a directive?

  11. 11

    how to pass data from controller to directive using $emit

  12. 12

    How Do I call and pass data to a Directive from a Controller Angular

  13. 13

    How to mock jQuery with Jasmine

  14. 14

    How to pass array of data to controller?

  15. 15

    How to Pass data from directive template to controller?

  16. 16

    angular + jasmine + mock $stateParams in a directive

  17. 17

    Pass ajax data from Controller to directive

  18. 18

    Angularjs how to pass data from a controller to a directive

  19. 19

    How do I inject a mock dependency into an angular directive with Jasmine on Karma

  20. 20

    Call method on Directive to pass data to Controller

  21. 21

    AngularJS: pass data to directive with existing controller

  22. 22

    angularjs: What is the preferred way to pass data from controller to directive(s)?

  23. 23

    Jasmine Test: How to mock a method inside a controller in AngularJS

  24. 24

    How to pass variable $data to controller?

  25. 25

    How to pass scope value to the directive controller

  26. 26

    How to pass data from directive to controller?

  27. 27

    How to pass data from controller to directive from ajax call AngularJS

  28. 28

    How to pass a method to a directive and invoke it to pass data to a nested directive?

  29. 29

    How to have a mock controller pass data to a directive with Jasmine

HotTag

Archive