模仿承诺与茉莉花

时间:2015-06-27 22:41:37

标签: javascript unit-testing mocking jasmine promise

我一直在努力进行一项简单的测试,包括嘲笑没有运气的承诺。我使用茉莉花间谍。希望有人可以帮助我。我已成功嘲笑findByUserName方法,但承诺似乎有问题。测试失败,没有任何错误。请看我的代码:

正在测试的模块CertificationSettingsManager.FindUser

    '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;

这是测试规范:

    '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();
      });

      //done callback passed so it can be asynchronous  
      it('if user has password, it should remove the password', function(done) {
        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', function(){
            expect(inputUserTemplate.password).toBeUndefined();
            // done being called
            done();
        });
      });
    });

第二个测试:it('if user has password, it should remove the password'失败,但是当我尝试调试测试时,它从未到达then内部:

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

**修订!!! ****

完成回调添加到 it 方法后,测试现在正在运行......这就是我所遗漏的内容。然后在期望语句之后调用 done 。 有没有更好的方法呢?

0 个答案:

没有答案