how to inject module dependencies in angular test unit with karma

1.5k Views Asked by At

it's the first time that i use angular application using generator and units tests with karma. i wanna create some angularjs unit test for my app based on bower, gulp, karma... this is my controller :

(function() {
    'use strict';

    angular.module('order', ['common'])

        .controller('OrderVehiculesController', function (drivers,Logger) {
            var self = this;
            this.orderForm = {
                id: '',
                txt: ''
            };
            this.msgCtrlError = {
                id: ''
            };
            var logger = Logger.getInstance('OrderVehiculesController');
            logger.log(self);


        this.getOrder = function (name) {

          // A modifier car il serait plus judicieux d'avoir un model d'object et de pouvoir les réinitialiser avec une function
          self.msgCtrlError.id = '';
          self.msgCtrlError.txt = '';

          drivers
            .getOrder(name)
            .then(function (orderData) {
              self.orderData = orderData;
              logger.log('orderData='+orderData);
              logger.log('orderData name='+orderData.name);
            })
            .catch(function (err) {
              logger.error('status=' + err.status + ' error=' + err.error);
              self.msgCtrlError.id = err.status;
              self.msgCtrlError.txt = err.error;
            });
        };
        });
})();

and the [common] is like that :

    (function() {
      'use strict';
      angular.module('common', [
        'common.drivers-service',
        'common.ads-auth-service',
        'common.liferay-service',
        'common.locale-service',
        'common.logging-service'
      ]);
    })();

and I tried to test it like that:

'use strict';
describe('orderModule', function () {
       // Set up the module
        beforeEach(module('common'));
        beforeEach(module('order'));
       var scope;
      //test controller order
     describe('OrderVehiculesController', function () {

       var controller;

       beforeEach(inject(function ($controller, $rootScope,common) {
           scope = $rootScope.$new();
           //TODO: how to call common mudule???
           //instantiation of controller with its modules
           controller = $controller('OrderVehiculesController', {
                $scope: scope,

           });
       }));

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

     });
 });

but finaly, i've this error and i didn't understand it, i need some help please. thank you.

ERROR is:

> WARN [watcher]: Pattern "C:\Users\src\**\*.mock.js" does not match any    file.

> INFO [PhantomJS 1.9.8 (Windows 7)]: Connected on socket 4vwNDK9jjnvHoFW7eeL-   with id 39484477
        PhantomJS 1.9.8 (Windows 7) orderModule OrderVehiculesController should be  defined FAILED
            Error: [$injector:unpr] Unknown provider: commonProvider <- common
            http://errors.angularjs.org/1.3.15/$injector/unpr? 

and my karma config is the default karma.confg.js :

'use strict';

module.exports = function(config) {

  var configuration = {
    autoWatch : false,

    frameworks: ['jasmine'],

    ngHtml2JsPreprocessor: {
      stripPrefix: 'src/',
      moduleName: 'gulpAngular'
    },

    browsers : ['PhantomJS'],

    plugins : [
      'karma-phantomjs-launcher',
      'karma-jasmine',
      'karma-ng-html2js-preprocessor'
    ],

    preprocessors: {
      'src/**/*.html': ['ng-html2js']
    }
  };

  // This block is needed to execute Chrome on Travis
  // If you ever plan to use Chrome and Travis, you can keep it
  // If not, you can safely remove it
  // https://github.com/karma-runner/karma/issues/1144#issuecomment-53633076
  if(configuration.browsers[0] === 'Chrome' && process.env.TRAVIS) {
    configuration.customLaunchers = {
      'chrome-travis-ci': {
        base: 'Chrome',
        flags: ['--no-sandbox']
      }
    };
    configuration.browsers = ['chrome-travis-ci'];
  }

  config.set(configuration);
};
0

There are 0 best solutions below