不一致地得到错误:[$ injector:modulerr]无法实例化模块

时间:2014-06-21 19:47:33

标签: node.js angularjs jasmine karma-runner

我从Angular / Karma / Jasmine获得的结果不一致。当我运行' npm test'时,我得到:

INFO [karma]: Karma v0.10.10 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 35.0.1916 (Linux)]: Connected on socket aW0Inld7aRhONC2vo04k
Chrome 35.0.1916 (Linux): Executed 1 of 1 SUCCESS (0.345 secs / 0.016 secs)

然后,如果我只是保存代码或测试文件(没有更改),它有时会产生相同的结果,有时会出错:

INFO [watcher]: Removed file "/home/www/raffler/entries.js".
Chrome 35.0.1916 (Linux) Raffler controllers RafflerCtrl should start with an empty masterList FAILED
Error: [$injector:modulerr] Failed to instantiate module raffler due to:
Error: [$injector:nomod] Module 'raffler' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

这不会造成任何变化。如果我停止Karma并重新启动它再次工作,但一旦失败它总是失败。是什么赋予了? Buggy Angular / Jasmine / Karma?代码和测试都是微不足道的。这是代码:

var myApp = angular.module('raffler', []);

myApp.controller('RafflerCtrl', function($scope, $log) {
  $scope.$log = $log;
  $scope.masterList = [];
});

这是测试:

'use strict';
describe('Raffler controllers', function() {
  describe('RafflerCtrl', function(){
    var scope, ctrl;
    beforeEach(module('raffler'));
    beforeEach(inject(function($controller) {
        scope = {};
        ctrl = $controller('RafflerCtrl', {$scope:scope});
    }));
    it('should start with an empty masterList', function() {
        expect(scope.masterList.length).toBe(0);
    });
  });
});

我做的事情是愚蠢的吗?似乎它应该给我一致的结果,不管我的愚蠢程度......谢谢。

1 个答案:

答案 0 :(得分:0)

你在问是否有错误。有。 Karma的作者知道文件观看存在问题。请参阅此问题:https://github.com/karma-runner/karma/issues/974

简单地保存文件而不进行更改可能会触发此行为。文件保存的主要方式有两种。第一种是删除原始文件(或将其重命名为.bak或其他内容),然后写出新内容。第二种方法将新内容写入临时文件,删除原始文件,然后将临时文件移动到原始文件所在的位置。对于这两者,文件系统监视可以触发一个事件,说某些文件/目录已更改。节点足够快,能够检测到文件被删除,并告诉Karma在测试中停止使用它。稍微少用的第三种方法是以特殊方式打开文件以覆盖内容,这将使Karma感到高兴。

回到那个bug ...在上面的场景中,当检测到文件系统更改时,不会重新评估globs。因此它认为您的文件已被删除。它从未看到添加过新文件,因此它现在已经不在测试套件中了。

这个错误也困扰着我。如果您有想法或拉取请求,那么我建议将其提供给Karma团队。已经有一个正在审核的补丁应该可以解决这些问题 - 请参阅https://github.com/karma-runner/karma/issues/1123

作为解决方法,您可以对vim使用“set backupcopy = yes”。其他编辑器中可能存在更改行为的设置,以便覆盖文件而不是替换。

相关问题