Error using Jasmine with AngularJS

I am using jasmin to test my AngularJs project with the following code

controllersSpec.js

describe('myApp', function(){ beforeEach(angular.mock.module('myApp')); it('should have a mailctrl', function() { expect(myApp.mailctrl).toBeDefined(); }); }); 

controller.js

 var myApp = angular.module('myApp', []); myApp.controller('mailctrl', ['$scope', '$routeParams', '$rootScope', 'getMailData', '$angularCacheFactory', function ($scope, $routeParams, $rootScope, getMailData, $angularCacheFactory) { ##....controller content....## } ]); 

SpecRunner.html

<script type="text/javascript" src="../lib/angular/angular.min.js"></script> <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script> <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script> <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script> <script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script> <script type="text/javascript" src="../test/lib/angular/angular-mocks.js"></script> <script src="../js/controller.js"></script> <script src="../test/unit/controllersSpec.js"></script>

Now when I open Specrunner.html in a browser, I get the following error

Expected undefined.
I have the following question regarding above

1) Where am I mistaken?
2) How can I use the controller $ rootScope variable?
3) What is the difference in beforeEach (angular.mock.module ('myApp')); and beforeEach (angular.module ('myApp')); and beforeEach (module ('myApp'));

EDIT:

I changed the code as

 describe('myApp', function(){ beforeEach(angular.mock.module('myApp')); it('should have a mailctrl', inject(function($rootScope, $controller) { var controller = $controller('mailctrl', { $scope: $rootScope.$new(), $rootScope: $rootScope }); expect(controller).toBeDefined(); })); 

and get the following error -

Error: [$ injector: unpr] http://errors.angularjs.org/undefined/ $ injector / unpr? p0 =% 24routeParamsProvider% 20% 3C-% 20% 24routeParams

If I add other parameters to $ controller () (i.e. '$ routeParams', 'getMailData'), the error occurs because they are undefined.

+3
source share
1 answer

1) You make a mistake trying to access the controller as a field in the module. in your test, you must enter $controller in your test to create your controller. This means that you should write your tests as follows:

 it('should have a mailctrl', inject(function($rootScope, $controller) { var controller = $controller('mainctrl', { $scope: $rootScope.$new(), $rootScope: $rootScope, ... }) // rest of your dependencies expect(controller).toBeDefined(); })); 

2) See 1

3) angular.mock.module used to register your module with the injector in your tests. This is the same as module . angular.module used to create, register, or retrieve a module for your application.

Regarding your editing:

You do not provide routeParams and any other service that your controller depends on, so it does not know where to get it and throws this error. The $ injector should know what to enter for each parameter that your controller requires. Your controller depends on

  • $rootScope : provided by angular
  • $scope : $ rootScope can be created. $ new ()
  • getMailData : This is your service, you must provide a layout for this
  • $routeParams : This is a built-in angular structure, but there is no dedicated layout, but since it is a very simple object, you can provide you with a layout for yourself.
  • $angularCacheFactory : This is a third-party service, and you should also give a layout for this.

So, in the end you should create your controller as follows:

 it('should have a mailctrl', inject(function($rootScope, $controller) { var controller = $controller('mainctrl', { $scope: $rootScope.$new(), '$routeParams': {}, // whatever it needs to be to be able to test your controller $rootScope: $rootScope, 'getMailData': getMailDataMOCK // this object is created by you in your test code '$angularCacheFactory': $angularCacheFactoryMOCK }) expect(controller).toBeDefined(); })); 

If you do not provide any of these arguments, it will try to get it from the $ injector and throw an exception if it is not found.

+7
source

All Articles