如何正确注入$ ionicHistory?

时间:2017-01-22 21:26:30

标签: angularjs ionic-framework jasmine karma-jasmine angular-mock

我试图使用茉莉花在离子应用程序控制器上运行单元测试。我的控制器接受$ ionicHistory作为参数。我使用角1和离子1。

Error: [$injector:unpr] Unknown provider: $ionicHistoryProvider <- $ionicHistory

尝试通过Karma运行测试时,我的单元测试失败并出现上述错误。有没有办法模拟$ ionicHistory?或者我如何正确注入离子历史?

单元测试

        describe('SignInController', function(){
          var scope, controllerMock, deferredLogin, SignInServiceMock, stateMock, ionicHistoryMock;

          beforeEach(module('common.module'));
          beforeEach(module('utils.module'));
          beforeEach(module('api-client.module'));
          beforeEach(module('sign-in.module'));

          beforeEach(inject(function ($rootScope, $controller, $q, $ionicHistory) {
            scope = $rootScope.$new();
            ionicHistoryMock = $ionicHistory;
            deferredLogin = $q.defer();

            SignInServiceMock = {
              authentication: jasmine.createSpy('authentication spy')
                .and.returnValue(deferredLogin.promise)
            };

            stateMock = jasmine.createSpyObj('$state spy', ['go']);

            controllerMock = $controller('SignInController', {
              $scope: scope,
              $state: stateMock,
              $ionicHistory: ionicHistoryMock,
              SignInService: SignInServiceMock
            });

          }));

          it('scope should be defined', function () {
            expect(scope).toBeDefined();
          });

          it('SignInServiceMock should be defined', function () {
            expect(SignInServiceMock).toBeDefined();
          });

          it('controllerMock should be defined', function () {
            expect(controllerMock).toBeDefined();
          });

        });

Karma配置

  module.exports = function(config) {
    config.set({

      // base path that will be used to resolve all patterns (eg. files, exclude)
      basePath: '',


      // frameworks to use
      // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
      frameworks: ['jasmine'],


      // list of files / patterns to load in the browser
      files: [
        '../www/lib/angular/angular.js',
        '../www/app/common/**',
        '../www/app/services/*.js',
        '../www/app/utils/*.js',
        '../www/app/sign-in/*.js',
        '../www/js/*.js',
        '../www/lib/angular-mocks/angular-mocks.js',
        '**/*tests.js'
      ],


      // list of files to exclude
      exclude: [
        '../www/app/common/language-translator/**',
        '../www/app/common/splash/update-data.json'
      ],


      // preprocess matching files before serving them to the browser
      // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
      preprocessors: {
      },


      // test results reporter to use
      // possible values: 'dots', 'progress'
      // available reporters: https://npmjs.org/browse/keyword/karma-reporter
      reporters: ['progress'],


      // web server port
      port: 9876,


      // enable / disable colors in the output (reporters and logs)
      colors: true,


      // level of logging
      // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
      logLevel: config.LOG_INFO,


      // enable / disable watching file and executing tests whenever any file changes
      autoWatch: true,


      // start these browsers
      // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
      browsers: ['Chrome'],
      //browsers: ['PhantomJS'],


      // Continuous Integration mode
      // if true, Karma captures browsers, runs the tests and exits
      singleRun: false,

      // Concurrency level
      // how many browser should be started simultaneous
      concurrency: Infinity
    })
  };

0 个答案:

没有答案