1

这是我第一次使用带有 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.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);
};
4

0 回答 0