如何在带有业力的角度测试单元中注入模块依赖关系

时间:2015-05-20 09:32:07

标签: angularjs gulp karma-runner bower gulp-karma

这是我第一次使用角度应用程序使用生成器和单位测试与业力。 我想基于bower,gulp,karma为我的应用程序创建一些angularjs单元测试... 这是我的控制器:

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

[common]就是这样:

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

我尝试这样测试:

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

     });
 });

但最后,我发现了这个错误,我对此并不了解,我需要一些帮助。 谢谢。

错误是:

> 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? 

我的karma配置是默认的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 个答案:

没有答案