0
votes

This is most likely me not knowing exactly how to use Karma. Say I have an angularJS boilerplate test like so

'use strict';

describe('Controller: MainCtrl', function () {

  // load the controller's module
  beforeEach(module('testingApp'));

  var MainCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller) {
    scope = {};
    MainCtrl = $controller('MainCtrl', {
      $scope: scope
    });
  }));

  it('should attach a list of awesomeThings to the scope', function () {
    expect(scope.awesomeThings.length).toBe(3);
  });
});

if I insert an $emit in my Controller like so

'use strict';

angular.module('testingApp')
  .controller('MainCtrl', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];

    $scope.$emit('helloWorld');
  });

My test now fails and says

TypeError: Object # has no method '$emit'
Any help is much appreciated!
1

1 Answers

1
votes

Instead of providing empty object as scope, use:

$scope = $rootScope.$new()

So your code would be:

// Initialize the controller and a mock scope
  beforeEach(inject(function ($controller) {
    scope = $rootScope.$new();
    MainCtrl = $controller('MainCtrl', {
      $scope: scope
    });
  }));