所以我在 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.
为什么即使放入两个测试似乎对于第一个测试(没有注入(inject))来说工作得很好 - 第二个测试仍然像前面所说的那样失败......
重要:如果我在浏览器窗口中运行应用程序,一切都会正常,因此错误必须位于测试文件或其他内容中:S
我对 Angular js 和 karma 相当陌生,所以我希望这不是一个愚蠢的问题。
最佳答案
尝试使用$httpBackend而不是$http。此外,注入(inject)和模拟应该在测试设置阶段的测试用例之外进行。对任何设置和实例化代码使用 beforeEach() block 。
关于javascript - Karma 错误 : [$injector:modulerr] Failed to instantiate module due to: (Index. html 工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175782/