得到一个错误:[$ injector:modulerr]无法在angularjs + karma + jasmine项目中实例化模块App

时间:2018-05-09 10:29:05

标签: angularjs karma-runner karma-jasmine

我们有一个包含多个控制器,工厂,配置等的庞大项目,最近我开始整合karma + jasmine来编写单元测试用例。但是我得到了上面的错误。我读了多个解决方案,并以多种方式注入了模块,但仍然遇到类似或多个错误。

我想测试我的工厂,控制器,配置,但即使遵循多个教程也没有解决我的问题,我也不理解它。

有人可以帮忙吗?

app.js

var App = angular.module('App', ['ngRoute', 'ngCookies','ngMaterial', 'infinite-scroll','ui.multiselect']);

App.factory("LS", ['$window', function($window) {
  return {
    setData: function(key, val) {
      $window.localStorage && $window.localStorage.setItem(key, val);
      return this;
    },
    getData: function(obj) {
      return $window.localStorage && $window.localStorage.getItem(obj);
    }
  };
}]);

app.spec.js

describe('LS factory', function() {
  var  LS;

  beforeEach(module('App'));

  beforeEach(inject(function() {
    var $injector = angular.injector(['App']);
    LS = $injector.get('LS');
  }));

  it('has a dummy spec to test 2 + 2', function() {
    expect(2+2).toEqual(4);
  });

});

karma.conf.js:

files: [
    './node_modules/angular/angular.js',    
    './node_modules/angular-route/angular-route.min.js',  // angular
    './node_modules/angular-cookies/angular-cookies.min.js',
    './node_modules/angular-material/angular-material.min.js',
    './node_modules/ng-infinite-scroll/build/ng-infinite-scroll.min.js',
    './assets/lib/js/multiselect.js',
    './node_modules/angular-ui-router/release/angular-ui-router.js', // ui-router
    './node_modules/angular-mocks/angular-mocks.js',
    './node_modules/angular-animate/angular-animate.min.js',
    './node_modules/angular-aria/angular-aria.min.js',
    './node_modules/jquery/dist/jquery.min.js',
    './node_modules/bootstrap/dist/js/bootstrap.min.js',
    './node_modules/moment/moment.js',
    './node_modules/lodash/lodash.js',
    './node_modules/owl.carousel/dist/owl.carousel.min.js',
    './node_modules/c3/c3.js',
    './app.js',
    './assets/common/*.js',
    './assets/configs/*.js',
    './assets/lib/adb/*.js',
    './admin-portal/*/*.js',
    './advertiser-portal/*/*.js',
    './publisher-portal/*/*.js',
    './app.spec.js'
    ],

我在karma.conf.js中包含了所有文件但仍然出现以下错误:

Chrome 66.0.3359 (Linux 0.0.0) LS factory has a dummy spec to test 2 + 2 FAILED
    Error: [$injector:modulerr] Failed to instantiate module App due to:
        at <Jasmine>
        at node_modules/angular/angular.js:125:12
        at node_modules/angular/angular.js:5034:15
        at forEach (node_modules/angular/angular.js:418:20)
        at loadModules (node_modules/angular/angular.js:4994:5)
        at Object.createInjector [as injector] (node_modules/angular/angular.js:4911:19)
        at UserContext.WorkFn (node_modules/angular-mocks/angular-mocks.js:3181:52)
        at <Jasmine>
Chrome 66.0.3359 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.025 secs / 0.018 secs)

有人可以帮我解决这个问题吗?

我使用角度1.6.10和业力2.0.2

1 个答案:

答案 0 :(得分:0)

显然,我导入ui.multiselect之一的依赖项导致错误。

  • 我删除了所有依赖项并运行karma test
  • 所以我开始通过在另一个依赖
  • 之后添加一个来检查它

最后发现问题是其中一个依赖项并将其删除。所有的测试用例现在都运行正常。