jasmine + angular ex 12

events

by ronapelbaum

HTML

<script src="https://jasmine.github.io/2.4/lib/jasmine.js"></script>
<script src="https://jasmine.github.io/2.4/lib/jasmine-html.js"></script>
<script src="https://jasmine.github.io/2.4/lib/boot.js"></script>
<link rel="stylesheet" href="https://jasmine.github.io/2.4/lib/jasmine.css">
<script src="https://code.angularjs.org/1.4.9/angular.js"></script>
<script src="https://code.angularjs.org/1.4.9/angular-mocks.js"></script>

JavaScript

//--------------BL------------
(function() {

  function MyController($scope) {
    var that = this;
    $scope.$on('greet', function() {
      that.data = 'Hello World';
    })
  }

  angular.module('MyModule', [])
    .controller('MyController', ['$scope', MyController]);
})();

//--------------specs------------
describe("MyController spec", function() {
  var ctrl, $rootScope;
  beforeEach(module('MyModule'));

  beforeEach(inject(function(_$rootScope_, $controller) {
    $rootScope = _$rootScope_;
    ctrl = $controller("MyController", {
      $scope: $rootScope.$new()
    });
  }));

  it("greeting should change after event", function() {
    expect(ctrl.data).not.toBeDefined();
    console.log(ctrl.data)
    $rootScope.$broadcast('greet');
    console.log(ctrl.data)
    expect(ctrl.data).toBe('Hello World');
  });

});