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) {
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.