[英]Karma Error: [$injector:modulerr] Failed to instantiate module due to: (Index.html is working perfectly)
So I set up an angular js project in webstorm and everything works properly except for the karma testing suite 因此,我在webstorm中设置了一个angular js项目,除业力测试套件外,其他所有功能均正常运行
I am setting up the model as follows: 我正在建立模型,如下所示:
beforeEach(module('[modulename]'));
and my tests are: 我的测试是:
it('should work', ((function() {
//spec body
expect(true).toBe(true);
})));
works and passes, but 工作并通过,但是
it('should work, does it?', (inject(function(_$http_) {
//spec body
$http=_$http_;
expect(true).toBe(true);
})));
not only fails the test but suddenly my module is no longer loading :S 不仅未通过测试,而且突然我的模块不再加载:S
Error: [$injector:modulerr] Failed to instantiate [modulename] due to: Error: [$injector:nomod] Module '[modulename]' is not available!
错误:[$ injector:modulerr]由于以下原因而无法实例化[modulename]:错误:[$ injector:nomod]模块'[modulename]'不可用! 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.
如果注册模块,请确保将依赖项指定为第二个参数。
why though even putting in both tests seems to work perfectly fine for the first test (without inject) - the second one still fails like previously stated... 为什么即使在第一次测试中都进行两个测试似乎都很好(没有注入),但第二次测试仍然失败,如前所述...
IMPORTANT: everything works if I run my application in a browser window, so the error has to be in the test file or something :S 重要提示:如果我在浏览器窗口中运行应用程序,则一切正常,因此错误必须出在测试文件中或:S
I'm rather new to angular js and karma so I hope this is not a stupid question. 我对angular js和karma不太熟悉,所以我希望这不是一个愚蠢的问题。
Try using $httpBackend instead of $http . 尝试使用$ httpBackend代替$ http 。 Also, injection and mocking should be done outside the test cases in the test setup phase.
另外,注入和模拟应该在测试设置阶段的测试用例之外进行。 use beforeEach() block for any setup and instantiation code.
对任何设置和实例化代码使用beforeEach()块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.