简体   繁体   中英

Unit testing using sequelize, mocha and async : Sequelize doesn't call back

I have a problem with mocha and async/sequelize I think.

I have a form that allow the user to enter his pseudo and password and do some asynchronous work with that. It works really fine. But I want to write unit testing for all my application.

When I wrote the test for this part, it doesn't work, because sequelize never call the success function back and I really don't know why, because it works without mocha.

Here is the code for the processing of the form :

var inscrire = function(data, cb){
//Getting the data
var pseudo = data.pseudonyme;
var password = data.password;
var passConfirm = data.passwordConfirmation;
//Verifying the form
//Pseudonyme
if(pseudo.length < 1 || password.length > 255){
    cb(null, 'form');
    return;
}
//Password
if(password.length < 1 || password.length > 255){
    cb(null, 'form');
    return;
}
//Password confirmation
if(passConfirm != password){
    cb(null, 'form');
    return;
}
async.waterfall([
    //Finding the user
    function(callback){
        //Find the user with the pseudonyme
        db.User.find({where : {'pseudonyme' : pseudo}}).done(function(err, user){
            console.log('AAAA');
            if(err){
                throw err;
            }
            console.log('YEAH');
            callback(null, user);
        });
    },
    //Creating the user if he's not here
    function(user, callback){
        //If the user is not in the base
        if(!user){
            //Hash the password
            password = hash(password);
            //Create the user
            db.User.create({'pseudonyme' : pseudo,
                            'password' : password}).success(function(){
                callback(null, true);
            });
        }else{
            //The user is alreadyhere
            callback(null, 'useralreadyhere');
        }
    }
], function(err, result){
    //Throw any exception
    if(err){
        throw err;
    }
    //Returning the result
    cb(null, result);
});

}

And here is the part of my unit test:

describe('#user-not-in-db', function() {
    it('should succeed', function(){
        var data = {
            'pseudonyme' : 'test',
            'password' : 'test',
            'passwordConfirmation' : 'test'
        };
        async.waterfall([
            function(callback){
                index.inscrire(data, callback);
            }
        ], function(err, result){
            console.log('YO');
            result.should.equal('YOO');
        });
    });
});

Thank you in advance.

I see at least one problem with the unit test as you have written it:

It's running as a synchronous test.

To run an async tests in mocha, the it test callback must take a "done" argument or return a promise. For example:

describe('foo', function(){
  it('must do asyc op', function(done){
    async.waterfall([
      function(cb){ setTimeout(cb,500); },
      function(cb){ cb(null, 'ok'); }
    ], function(err, res){
         assert(res);
         done();
       }
    );
  });
});

See part of the mocha documentation for more examples:

http://visionmedia.github.io/mocha/#asynchronous-code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM