I am using jasmin to test my AngularJs project with 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 am opening Specrunner.html in browser, I am getting following error
Expected undefined to be defined.
I have following question regarding above
1) Where I am getting wrong ?
2) How can I use $rootScope variable of controller ?
3) Whats the difference in beforeEach(angular.mock.module('myApp')); and beforeEach(angular.module('myApp')); and beforeEach(module('myApp'));
EDIT :
I have modified 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 getting following error -
Error: [$injector:unpr] http://errors.angularjs.org/undefined/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams
If I add other params in $controller() (i.e. '$routeParams', 'getMailData') Error comes as they are undefined.