0

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:

I suppose I have a configuration issue somewhere but I'm unable to solve it.

Does anyone have an idea?

Candlejack
  • 448
  • 10
  • 23
Patrick Lec
  • 173
  • 1
  • 1
  • 11

1 Answers1

0

I suspected that my previous test code did not work with my existing library versions. I'm using:

"devDependencies": {
"bower": "^1.8.0",
"http-server": "^0.9.0",
"jasmine-core": "^2.4.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-firefox-launcher": "^1.0.0",
"karma-jasmine": "^1.0.2",
"phantomjs": "^2.1.7",
"protractor": "^4.0.9"

After further investigation I rewrote the test code as follows:

    describe('controller register module unit tests implementation', function(){

    var $controller;
    var $scope;
    var userRestService;

    beforeEach(module('ghhweb.register.controller', function($provide){
        userRestService = jasmine.createSpyObj('userRestService'
            , ['checkExistingMail']);

        userRestService.checkExistingMail.and.returnValue(0);

        $provide.value('userRestService', userRestService);
    }));


    beforeEach(inject(function(_$controller_, $rootScope,_userRestService_){
        $scope = $rootScope.$new();
        userRestService = _userRestService_;
        $controller = _$controller_('RegisterController', {
            $scope: $scope,
            userRestService: userRestService
        });
        $scope.callCheckExistingMail();
    }));

    it('should call userRestService', function(){
        expect(userRestService.checkExistingMail).toHaveBeenCalled();
    });
});

The test now runs correctly!

Candlejack
  • 448
  • 10
  • 23
Patrick Lec
  • 173
  • 1
  • 1
  • 11