Grunt-Karma-Jasmine:找不到模块

Grunt-Karma-Jasmine: can not find module

我正在尝试为我的 Angular.js 项目设置测试,但我不断收到“$injector:nomod,模块 'result' 不可用!你拼错了...”错误。我确定我在 "karma.config.js" 内的 "files" 数组中包含 "result" 模块,基本上它看起来像这样:

files: [
          '../javascripts/jquery-2.1.4.min.js',
          '../jquery-ui/jquery-ui.min.js',
          '../D3/d3.js',
          'libs/angular.min.js',
          'libs/angular-route.min.js',
          'libs/angular-animate.min.js',
          'libs/selectize.js',
          'libs/angular-selectize.js',
          'libs/angular-mocks.js',

          'simulator.js',

          '*.js',
          'services/**/*.js',
          'qa/tests-*.js'
],

...

我一开始以为主模块的顺序:'simulator'(在'simulator.js'文件中定义)是错误的,所以我特意向上移动了它,之前 其他模块,如以下 Whosebug 线程推荐:

Angular module not available in Karma Jasmine test run

没用。然后我尝试确保文件的导入顺序与我的 angular 应用程序的主入口文件中的顺序相同(angular-mocks.js 和 qa/tests-* 除外.js), 导入每个单独的文件,而不是使用通配符,但没有成功。

Jasmine 确实进入了测试文件,但偶然发现了我试图导入模块的行 "result":

describe('simulator.chartService', function() {

var chartService;
var graphConfig;

console.log("instantiating module result");
  beforeEach(module('result'));
console.log("finished instantiating");

beforeEach(inject(function($injector) {
  graphConfig = $injector.get('graphConfig');
  chartService = $injector.get('chartService');

}));

it('should be created', function() {
  expect(chartService.calcColors(10)).not.toBeNull();
});

});

所以,我看到错误发生在两个 console.log() 语句之间。

我怀疑 "karma.config.js" 中数组 "files" 中的文件排序仍然有问题。我有主模块 "simulator",它依赖于其他模块:

angular.module('simulator', ['ngRoute','ngAnimate','selectize','newexp2','newexp','login','edit','exps', 'result','templates','commons'])

模块'newexp2'、'newexp'、'login'、'edit'、'exps'、'result'、'templates'都是依赖的在模块 'commons'.

如何在"files"数组中正确导入相互依赖的模块? 是否足以将 "simulator.js"、主模块置于所有其他模块之上, 或者我还需要在 "commons.js"?

之前放置所有其他模块

我的另一个怀疑是,我从 angular 官方网站 "angular-mocks.js" 下载的 angular.js 库版本可能与我正在使用的其他模块不兼容。我之前遇到过 "angular-animate.js" 文件的问题。

只要我用 $(function(){...}) 包围我的测试代码(并且我的所有其他模块都被它包围)它在导入 result 模块时不会产生错误,所以我开始看到两个 console.log() 语句之间没有错误,但是,这会产生一些未知错误,使我根本无法调用 it 部分,而当我没有用 $(function(){...}) 包围它时, it 测试被调用,但是模块 result 导入失败。

到目前为止,我几乎被困住了,不知道该往哪里移动,也不知道该尝试什么。任何建议将不胜感激。

好的,我明白了。问题是我的所有 angular 代码都包含在 $(function(){...}) 中。解决方案是删除所有 $function(){...}),然后在主条目 .html 文件中重新排序 javascript 导入,然后所有测试开始正常运行。 将问题标记为重复可能更好:

Angular document.ready() issue