【问题标题】:Grunt-Karma-Jasmine: can not find moduleGrunt-Karma-Jasmine:找不到模块
【发布时间】:2015-10-28 06:01:59
【问题描述】:

我正在尝试为我的 Angular.js 项目设置测试,但我不断收到“$injector:nomod, Module 'result' is not available! You or mispelled...”错误。我确定我在“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' 文件中定义)是错误的,所以我特意将它向上移动,之前 其他模块,如以下stackoverflow线程推荐:

Angular module not available in Karma Jasmine test run

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

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

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”中的排序仍然有问题。我有依赖于其他模块的主模块“模拟器”:

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 测试,但是模块@ 987654331@导入失败。

到目前为止,我几乎被卡住了,不知道该去哪里以及该尝试什么。任何建议将不胜感激。

【问题讨论】:

  • 您确定simulator 是缺少的模块吗?也许它是您要注入的其他模块之一。
  • 不,根据它给我的错误,它没有丢失。缺少主模块“模拟器”所依赖的“结果”模块
  • 我在终端中运行 grunt,它显示有一个错误 b/w 两个 console.log() 语句。我点击带有错误的链接,它显示以下$injector:modulerrFailed to instantiate module result due to: [$injector:nomod] http://errors.angularjs.org/1.4.7/$injector/nomod?p0=result I/<@http://localhost:9876//home/nikita/workspace/simulator/public/angular/libs/angular.min.js?b6b56d0e096efc26e09d6cf2cd37aac6123cc346:6:416 de/workFn@http://localhost:9876/base/libs/angular-mocks.js?e...点击链接进一步给我$injector:nomodModule 'result' is not available! ...
  • 你在哪里定义result 模块?
  • result.js 文件中,该文件与karma.config.js 文件位于同一文件夹中,因此我可以手动导入它,就像我对simulator.js 所做的那样,或者使用通配符:*.js,这是我在导入 simulator.js 文件后立即执行的操作。

标签: angularjs karma-jasmine gruntfile


【解决方案1】:

好的,我想通了。问题是我所有的角度代码都包含在$(function(){...}) 中。解决方案是删除所有$function(){...}),然后在主条目 .html 文件中重新排序 javascript 导入,然后所有测试开始正常工作。 这个问题可能会更好地标记为重复:

Angular document.ready() issue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2015-12-06
    • 2014-11-18
    • 2015-03-07
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多