Karma Error: [$injector:modulerr] Failed to instantiate module due to: (Index.html is working perfectly)

Karma Error: [$injector:modulerr] Failed to instantiate module due to: (Index.html is working perfectly)

所以我在 webstorm 中设置了一个 angular js 项目,除 karma 测试套件外,一切正常

我正在设置模型如下:

beforeEach(module('[modulename]'));

我的测试是:

it('should work', ((function() {
        //spec body
        expect(true).toBe(true);
    })));

工作并通过,但是

it('should work, does it?', (inject(function(_$http_) {
        //spec body
        $http=_$http_;
        expect(true).toBe(true);
    })));

不仅测试失败而且我的模块突然不再加载:S

Error: [$injector:modulerr] Failed to instantiate [modulename] due to: Error: [$injector:nomod] Module '[modulename]' 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.

为什么即使将两个测试都放入第一个测试(没有注入)似乎工作得很好 - 第二个测试仍然如前所述失败...

重要提示:如果我 运行 我的应用程序在浏览器 window 中,一切正常,所以错误必须在测试文件或其他东西中:S

我对 angular js 和 karma 比较陌生,所以我希望这不是一个愚蠢的问题。

尝试使用 $httpBackend 而不是 $http。此外,注入和模拟应该在测试设置阶段的测试用例之外完成。将 beforeEach() 块用于任何设置和实例化代码。