[英]Unit testing using sequelize, mocha and async : Sequelize doesn't call back
我认为摩卡咖啡和异步/续集有问题。
我有一个允许用户输入其伪密码和密码并以此做一些异步工作的表格。 它真的很好。 但是我想为所有应用程序编写单元测试。
当我为该部分编写测试时,它不起作用,因为sequelize永远不会调用成功函数,而且我真的不知道为什么,因为它没有mocha就可以工作。
这是处理表格的代码:
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);
});
}
这是我的单元测试的一部分:
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');
});
});
});
先感谢您。
在编写单元测试时,我发现至少有一个问题:
它正在作为同步测试运行。
要在mocha中运行异步测试, it
测试回调必须采用“完成”参数或返回承诺。 例如:
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();
}
);
});
});
有关更多示例,请参见mocha文档的一部分:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.