samedi 27 juin 2015

Mock promises with jasmine

I've been struggling with a simple test that involves mocking promises with no luck. I'm using jasmine spies. Hope somebody can help me. I've successfully mocked findByUserName method but there appears to be something wrong with the promise. The tests just fails without any error. Please see my code:

Module CertificationSettingsManager.FindUser being tested:

    'use strict';

    var CertificationSettingsManagerFindUser = function(usersRepository, accountsRepository, userName, next) {

      if (userName) {
        usersRepository.findByUserName(userName)
                .then(function(user) {
                  if (user && user.password) {
                    delete user.password;
                  }
                  return user;
                })
                .then(function(user) {
                  if (user) {
                    accountsRepository.findAllAssignedTo(user.userId).then(function(results) {
                      user.hasAccountsAssigned = (results.count > 0);
                      user.belongsRepHierarchy = true;
                      user.isMemberOfReconOrPrep = true;
                      next(null, user);
                    });
                  }
                })
                .catch(function(err) {
                  if (err) {
                    next(err);
                  }
                });
      } else {
        next();
      }
    };

    module.exports = CertificationSettingsManagerFindUser;

And this is the test spec:

    'use strict';

    var findUserModule = require('./CertificationSettingsManager.FindUser');
    var Q = require('q');

    describe('CertificationSettingsManager findUser module', function() {

      var userSpy;
      var accounstSpy;
      var nextCallbackSpy;

      var inputUserTemplate;
      var accountsResult;

      beforeEach(function() {
        inputUserTemplate = {
          userId: 1234
        };

        accountsResult = {
          count: 0
        };

        userSpy = jasmine.createSpyObj('UserRepository', ['findByUserName']);
        accounstSpy = jasmine.createSpyObj('ProfileRepository', ['findAllAssignedTo']);
        nextCallbackSpy = jasmine.createSpy('nextCallback spy');

      });

      it('when userName was not supplied it should call next with no parameters', function() {
        findUserModule(null, null, null, nextCallbackSpy);
        expect(nextCallbackSpy).toHaveBeenCalledWith();
      });

      it('if user has password, it should remove the password', function() {
        inputUserTemplate.password = 'there is some password here';

        accounstSpy.findAllAssignedTo.and.returnValue(Q.resolve(accountsResult));
        userSpy.findByUserName.and.returnValue(Q.resolve(inputUserTemplate));

        findUserModule(userSpy, accounstSpy, 'rassiel', nextCallbackSpy);

        expect(inputUserTemplate.password).toBeUndefined();
      });
    });

The second test: it('if user has password, it should remove the password' is failing, but when I try to debug the test it never hits the then inside:

        usersRepository.findByUserName(userName)
                .then(function(user) {

the tests just suddenly ends and fails. I don't know what I'm missing. Thanks!

Aucun commentaire:

Enregistrer un commentaire