1

Hello I have this controller that handles login:

angular.module('fancyApp')
.controller('MainCtrl', function ($scope, $location, $http, LoginFactory) {
 $scope.loginForm = function(isValid) {

if (isValid) {
 var status;

 LoginFactory.login($scope.person).then(function(d) {
  console.log(d);
  $scope.data = d;

  if (d.status === 200) {
      //login success
      $('.loginFail').hide();
      $location.path('/student');

    } else {
      $scope.loginFail = 'Failed to login';
      $('.loginFail').show();
    }

  });

  } else {
  $scope.login_form.submitted = true;
 }
 };
});

And this is my LoginFactory login function:

angular.module('fancyApp')
.factory('LoginFactory', function ($http, $q) {
return {
  login: function(user) {
    var promise = $http.post( 'api/v1/login', user).then(function (response) {
      return response;
    }, function(error) {
      return error;
    });
    return promise;
  };
});

This is my beforeEach:

beforeEach(inject(function ($controller, $provide, $location, $rootScope) {
location = $location;
rootScope = $rootScope;

var testService = {
  getStudents: function() {
    return ['student1', 'student2'];
  },
  getAdmins: function() {
    return ['admin1', 'admin2'];
  },
  login: function(person) {
    console.log(testService.getStudents().indexOf(person.user));
    if(testService.getStudents().indexOf(person.user) !== -1) {
      return {status:200, token:'xxx', role:'student'};
    }
    else if(testService.getAdmins().indexOf(person.user) !== -1) {
      return {status:200, token:'xxx', role:'admin'};
    }
    else {
      return {status:401, token:'xxx', role:'student'};
    }
  }
};

scope = $rootScope.$new();
$provide.service('LoginService', testService);
MainCtrl = $controller('MainCtrl', {
  $scope: scope
});
}));

And here is my test:

it('Login should succeed', inject(function($httpBackend){
    spyOn(testService, 'login');

    scope.person.user = 'student1';
    scope.person.pass = '123456';
    scope.login(true);
    expect(testService.login).toHaveBeenCalledWith('student1', '123456');

  }));

The error I get is:

Error: [$injector:unpr] Unknown provider: $provideProvider < $provide

I'm not sure if this is the correct way of testing this, any suggestions would be nice.

Thank you.

1 Answer 1

3

You can't use $provide within the inject function because the former registers providers for the latter to use. Do it like this:

describe('...', function() {
    beforeEach(function() {
        module(function($provide) {
           $provide.service('LoginService', testService);
        });

        inject(function(someValue) {
            //Rest of the code within the inject..........
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Just make sure the "module" is before the "inject" or you'll get an error "Injector already created, can not register a module!"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.