Karma/Jasmine 不会注入离子控制器

Karma/Jasmine won't inject ionic controller

我正在尝试为离子项目设置测试,我的控制器测试如下:

describe('ShopsCtrl', function() {

  var controller;

  beforeEach(module('shoplocal'));

  it('should foo', function() {
    expect(true).toBe(true);
  });

  beforeEach(angular.mock.inject(function($controller) {
    // instantiate shop controller
    controller = $controller('ShopsCtrl');
  }));

  it('foo should be bar', function() {
    expect(controller.foo).toBe('bar');
  });
})

应用定义如下:

(function() {
  shoplocalRun.$inject = ['$ionicPlatform', '$rootScope'];
  function shoplocalRun($ionicPlatform, $rootScope) {
    $ionicPlatform.ready(function() {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        cordova.plugins.Keyboard.disableScroll(true);
      }

      if (window.StatusBar) {
        StatusBar.overlaysWebView(true);
        StatusBar.style(1);
      }
    });
  }

  angular.module('shoplocal', ['ionic', 'ngCordova'])
    .run(shoplocalRun);
})();

控制器位于 www/app/shops/shopsCtrl.js 中的单独文件中:

(function() {
  ShopsCtrl.$inject = [];
  function ShopsCtrl() {
    var vm = this;;

    vm.foo = "bar";
  }

  angular.module('shoplocal')
    .controller('ShopsCtrl', ShopsCtrl);
})();

然而,当我 运行 测试时,我从业力中得到的是:

PhantomJS 2.1.1 (Mac OS X 0.0.0) ShopsCtrl should foo FAILED
    /Users/ospfranco/marcus/ShopLocal-Ionic/www/lib/ionic/js/ionic.bundle.js:17698:53
    forEach@/Users/ospfranco/marcus/ShopLocal-Ionic/www/lib/ionic/js/ionic.bundle.js:13648:24
    loadModules@/Users/ospfranco/marcus/ShopLocal-Ionic/www/lib/ionic/js/ionic.bundle.js:17658:12
    createInjector@/Users/ospfranco/marcus/ShopLocal-Ionic/www/lib/ionic/js/ionic.bundle.js:17584:22
    workFn@/Users/ospfranco/marcus/ShopLocal-Ionic/bower_components/angular-mocks/angular-mocks.js:2393:60
PhantomJS 2.1.1 (Mac OS X 0.0.0) ShopsCtrl foo should be bar FAILED
    /Users/ospfranco/marcus/ShopLocal-Ionic/www/lib/ionic/js/ionic.bundle.js:17698:53
    forEach@/Users/ospfranco/marcus/ShopLocal-Ionic/www/lib/ionic/js/ionic.bundle.js:13648:24
    loadModules@/Users/ospfranco/marcus/ShopLocal-Ionic/www/lib/ionic/js/ionic.bundle.js:17658:12
    createInjector@/Users/ospfranco/marcus/ShopLocal-Ionic/www/lib/ionic/js/ionic.bundle.js:17584:22
    workFn@/Users/ospfranco/marcus/ShopLocal-Ionic/bower_components/angular-mocks/angular-mocks.js:2393:60
    TypeError: undefined is not an object (evaluating 'controller.foo') in /Users/ospfranco/marcus/ShopLocal-Ionic/tests/unit-tests/shops.controller.tests.js (line 27)
    /Users/ospfranco/marcus/ShopLocal-Ionic/tests/unit-tests/shops.controller.tests.js:27:22

知道发生了什么事吗?

原来我缺少 cordova 依赖项,我将 cordova 文件添加到我的测试 conf 中:

files: [
      '../www/lib/ionic/js/ionic.bundle.js',
      '../www/lib/ngCordova/dist/ng-cordova.js',
      '../www/app/**/*.js',
      '../bower_components/angular-mocks/angular-mocks.js',
      'unit-tests/**/*.js'
    ],

现在一切正常。