为什么茉莉花中的spyOn因TypeError而失败:' undefined'不是一个功能

时间:2015-05-25 13:26:59

标签: angularjs jasmine

我有一个控制器

'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();
  }

}());

测试是:

'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();
  });

});

当我进行测试时,我收到了错误:

PhantomJS 1.9.8(Windows 7)控制器:MainController应该调用启动FAILED TypeError:' undefined'不是一个函数(评估' spyOn(MainControll 呃,'启动')。和CallThrough()')

其他测试,例如期望标题是“你好”#39;工作得很好,有什么问题? 请记住,我正在使用johnpapa惯例'控制器As'没有注入范围。

1 个答案:

答案 0 :(得分:-1)

替换为 spyOn(MainController,' launch')。and.CallThrough();

请参阅:Jasmine 2.0文档和升级到2.0