当尝试使用Jasmine和AngularJS进行单元测试时,参数'fn'不是函数

[英]Argument 'fn' is not a function, when trying to do unit test with Jasmine and AngularJS


I'm trying to do some unit tests with Jasmine in my Angular application, but I'm facing some errors.

我正在尝试在我的Angular应用程序中使用Jasmine进行一些单元测试,但我遇到了一些错误。

Error

错误

Error: [$injector:modulerr] Failed to instantiate module LocalStorageModule due to:
Error: [ng:areq] Argument 'fn' is not a function, got string

Spec

规格

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

App.js

angular
    .module('app', ['ngRoute', 'LocalStorageModule'])
    .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
       // My routeProvider here
    }]);

I'm not using neither Yeoman nor Karma yet, because this is my fisrt application using Angular.

我还没有使用Yeoman和Karma,因为这是我使用Angular的fisrt应用程序。

Included Files

包含的文件

<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>

1 个解决方案

#1


15  

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.

我认为您的配置可能只是针对测试的依赖项存在问题。没有Karma,我不知道你是如何进行测试的,但我猜你有一个Jasmine的配置文件,你可以在其中指定要包含的文件。您必须包含所有文件,并且必须首先包括所有库。请小心订单,并尝试尊重您用于运行应用程序的html文件中的相同内容。

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.

如果订单错误,JS将尝试在包含它所需的库之前执行。在你的情况下甚至可能是整个角度堆栈。

Update

更新

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

不要忘记Jasmine使用它自己的html文件工作,并且如果你不告诉它,它将不包括你经常使用的库。而且不要忘记包括Angular Mock库,这对您的测试至关重要

Update 2

更新2

Ok I think I found why you have a problem

好吧我想我找到了你有问题的原因

There is something wrong in this code

这段代码有问题

beforeEach(module('app', ['ngRoute', 'LocalStorageModule']));

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.

当您使用模块('smthg',[])调用模块时,您正在创建它,而不是调用它。您应该使用此表单,而无需重新导入已包含在主模块中的服务。

beforeEach(module('app'));

check the Creating versus Retrieval section of the angular documentation https://docs.angularjs.org/guide/module

检查角度文档https://docs.angularjs.org/guide/module的“创建与检索”部分


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/12/16/6f7eda46ed471089113f9008e17f0368.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告