I'm starting with AngularJs and I'm facing an issue when trying to unit test a controller that has a service as a dependency. The application works as expected, but not the unit test. I got the following error with the Jasmine test:
Error: [$injector:modulerr] Failed to instantiate module ghhweb.register due to: Error: [$injector:unpr] Unknown provider: $stateProvider
My code for the module:
var registerModule = angular.module('ghhweb.register',[]);
registerModule.config(function($stateProvider, $locationProvider){
var registerState = {
name: 'register',
url: '/register',
controller: 'RegisterController',
templateUrl: 'modules/register/views/register.html'
};
$stateProvider.state(registerState);
});
My code for the Controller:
var registerController = angular.module('ghhweb.register.controller',[]);
registerController.controller('RegisterController', ['$scope',
'userRestService', function($scope, userRestService){
$scope.callCheckExistingMail = function(){
$scope.userEmail = userRestService.checkExistingMail($scope.userEmail);
};
}]);
My specification code :
describe('controller register module unit tests', function(){
describe('check user service implementation', function(){
beforeEach(module('ghhweb.register'));
it('should call checkExistinMail in userRestService', inject(function(
$rootScope, $controller){
// make a userRestService
var userRestService = {
checkExistingMail: function() {}
};
spyOn(userRestService, "checkExistingMail");
controller('RegisterController', {$scope: scope,
userRestService: userRestService });
expect(userRestService.checkExistingMail).toHaveBeenCalled();
}));
});
});
I have tried lots of tutorials and checked several posts like these:
- angular-unit-test-controllers-mocking-service-inside-controller
- injecting-a-mock-into-an-angularjs-service
I suppose I have a configuration issue somewhere but I'm unable to solve it.
Does anyone have an idea?