尝试使用 Jasmine 和 AngularJS 进行单元测试时,参数“fn"不是函数 [英] Argument 'fn' is not a function, when trying to do unit test with Jasmine and AngularJS

查看:10
本文介绍了尝试使用 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 的第一个应用程序.

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']));

当您使用 module('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'));

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

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

这篇关于尝试使用 Jasmine 和 AngularJS 进行单元测试时,参数“fn"不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆