mardi 12 janvier 2016

Getting error when mocking an AngularJS 1 service

When I test this code (from book: Angularjs services, By: Jim Lavin)

describe("authentication service", function(){
  var data;
  var service;
  var q;
  var deferred;
  var rootScope;

  angular.module('test.data', []).factory('data_service', 
      function (q) {
      data = {
          retrieveUser: function (email) {
              deferred = q.defer();
              return deferred.promise;
          }
      };

      return data;
  });

  beforeEach(module('application.services'));
  beforeEach(module('test.data'));

  beforeEach(inject(function (authenticate data_service, $rootScope, $q) {
      rootScope = $rootScope;
      service = authenticate;
      q = $q;
  }));

  it("should call the retrieveUser", function(){
      spyOn(data, "retrieveUser").and.callThrough();;

      var email = 'test.user@nomail.com';
      service.checkUser(email); 

      deferred.resolve({userName: email, 
      firstName: Faker.Name.firstName(), 
      lastName: Faker.Name.lastName()});

      rootScope.digest();

      expect(data.retrieveUser).toHaveBeenCalled();
  });
});

I get an error like this:

Error: [$injector:unpr] Unknown provider: qProvider <- q <- data_service.....

My goal is to mock a service used by a controller, that (the service) return a promise. Thank you.

Aucun commentaire:

Enregistrer un commentaire