mardi 8 septembre 2015

jasmine testing and angular promises with $q

I have a problem testing some promises I created in an angular service using $q.

angular.module('myApp')
  .service('userService', function ($q, $db, $rootScope) {
    this.register = function(newUser, password) {
      return $q(function(resolve, reject) {
        $db.User.register(newUser, password)
          .then(
            function(success) {
              resolve(success);
            },
            function(error) {
              reject(error);
            }
          );
      });
    };
  });

Part of the test

beforeEach(module('eMergencyApp'));

beforeEach(function(done) {
  inject(function (_userService_, $db) {
    userService = _userService_;
    $db.ready()
      .then(function() {
        $db.User.logout()
          .then(done);
      });
  });
});

it('should register and loggin a new user', inject(function($rootScope, $db, $q) {
  var testValue;
  var promise = userService.register(newUser, "angular-test");
  promise.then(function() {
    testValue = "bla";
  });
  $rootScope.$apply();
  expect(testValue).toBe("bla");
}));

But for some reason it does not work - the promise is never resolved. "testValue" is only a placeholder. I have to wait for the $db service first, so this is done in the beforeEach. After that i simply use the service and try to resolve the promise like it's done in the angular examples (http://ift.tt/1iD2D7J$q).

Aucun commentaire:

Enregistrer un commentaire