当尝试使用Jasmine和AngularJS进行单元测试时,参数'fn'不是函数
我正在尝试在我的Angular应用程序中使用Jasmine进行一些单元测试,但是我遇到了一些错误。
I'm trying to do some unit tests with Jasmine in my Angular application, but I'm facing some errors.
错误
Error: [$injector:modulerr] Failed to instantiate module LocalStorageModule due to:
Error: [ng:areq] Argument 'fn' is not a function, got string
规格
describe("testing the controller", function () {
var $controllerConstructor;
var scope;
beforeEach(module('app', ['ngRoute', 'LocalStorageModule']));
beforeEach(inject(function ($controller, $rootScope) {
$controllerConstructor = $controller;
scope = $rootScope.$new();
}));
it("should validate a contact", function () {
var ctrl = $controllerConstructor('crmContatosCtrl', { $scope: scope });
});
});
App.js
angular
.module('app', ['ngRoute', 'LocalStorageModule'])
.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
// My routeProvider here
}]);
我还没有使用Yeoman和Karma,因为这是我使用Angular的fisrt应用程序。
I'm not using neither Yeoman nor Karma yet, because this is my fisrt application using Angular.
包含的文件
<script src="../../Scripts/jasmine/jasmine.js"></script>
<script src="../../Scripts/jasmine/jasmine-html.js"></script>
<script src="../../Scripts/jasmine/boot.js"></script>
<script src="../../Scripts/angular/angular.js"></script>
<script src="../../Scripts/angular/angular-mocks.js"></script>
<script src="../../Scripts/angular/angular-route.js"></script>
<script src="../../Scripts/angular/angular-local-storage.js"></script>
<script src="../../Scripts/ngStorage.js"></script>
<script src="../../Scripts/ng-infinite-scroll.js"></script>
<script src="../../Scripts/angular/common.js"></script>
<link href="../../Scripts/jasmine/jasmine.css" rel="stylesheet" />
<script src="../core/app.js"></script>
<script src="../crm/contatos.js"></script>
<script src="contatosSpec.js"></script>
我认为您的配置可能有问题对于测试的依赖项。没有Karma,我不知道你是如何进行测试的,但我猜你有一个Jasmine的配置文件,你可以在其中指定要包含的文件。您必须包含所有文件,并且必须首先包括所有库。小心订单,并尝试尊重用于运行应用程序的html文件中的相同内容。
I think you may simply have a problem with your configuration for the dependencies for the tests. Without Karma I don't know how you do the tests but I guess you have somewhere a configuration file for Jasmine where you specify the files to be included. You have to include all files and you have to include first all the libraries. Be careful with the order and try to respect the same as you have in your html file that you use to run the application.
如果订单错误,JS会尝试在包含它所需的库之前执行。在你的情况下甚至可能是整个角度叠加。
If the order is wrong the JS will try to execute before the libraries it needs are included. In your case maybe even the whole angular stack.
更新
不要忘记Jasmine使用它自己的html文件工作,并且如果你不告诉它,将不包括你经常使用的库。并且不要忘记包括Angular Mock库,这对您的测试至关重要
Do not forget that Jasmine works using it's own html file and will not include the libraries you usually use if you do not tell it. And also don't forget to include Angular Mock library , essential for your tests
更新2
好的我觉得我找到了你遇到问题的原因
Ok I think I found why you have a problem
这段代码有问题
beforeEach(module('app', ['ngRoute', 'LocalStorageModule']));
当您使用模块('smthg',[])调用模块时,角度为创建,而不是调用它。您应该使用此表单,而无需重新导入已包含在主模块中的服务。
In angular when you call a module using module('smthg',[]) you are creating it, not calling it. You should use this form instead, there is no need to reimport the services that you already included in your main module.
beforeEach(module('app'));
检查角度文档的创建与检索部分https://docs.angularjs.org/guide/module
check the Creating versus Retrieval section of the angular documentation https://docs.angularjs.org/guide/module