Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

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.

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>
like image 744
Lucas_Santos Avatar asked Dec 16 '14 16:12

Lucas_Santos


Video Answer


1 Answers

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.

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.

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

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

beforeEach(module('app'));

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

like image 66
sam Avatar answered Sep 28 '22 01:09

sam