1
votes

I have a controller

'use strict';

(function(){

  /**
   * @ngdoc function
   * @name providersApp.controller:MainController
   * @description
   * # MainController
   * Controller of the providersApp
   */
  angular.module('providersApp')
    .controller('MainController', MainController);

  MainController['$inject'] = ['unicornLauncher'];

  function MainController(unicornLauncher){
    var vm = this;
    vm.title = 'Hello';
    vm.Level = unicornLauncher.launchedCount;
    vm.isHappy = true;
    vm.launch = launch;

    function launch(){
      unicornLauncher.launch();
      vm.Level = unicornLauncher.launchedCount;
    }


    unicornLauncher.launch();
  }

}());

And the testing is :

'use strict';

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

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

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


  it('should call launch', function () {
    spyOn(MainController, 'launch').andCallThrough();
    MainController.launch();
    expect( MainController.launch).toHaveBeenCalled();
  });

});

As i run the test I'm getting an error :

PhantomJS 1.9.8 (Windows 7) Controller: MainController should call launch FAILED TypeError: 'undefined' is not a function (evaluating 'spyOn(MainControll er, 'launch').andCallThrough()')

Other test such as expecting title to be 'Hello' works just fine, what is the problem ? Keep in mind I'm using the johnpapa convention 'controller As' with no scope injected.

1

1 Answers

-1
votes

Replace with spyOn(MainController, 'launch').and.CallThrough();

See: Jasmine 2.0 documentation and Upgrading to 2.0