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

时间:2016-04-16 21:30:43

标签: angularjs ionic-framework karma-jasmine

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

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

知道发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

原来我错过了cordova的依赖,我将cordova文件添加到我的测试中:

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'
    ],

现在一切正常。