【问题标题】:Unit testing using sequelize, mocha and async : Sequelize doesn't call back使用 sequelize、mocha 和 async 进行单元测试:Sequelize 不会回调
【发布时间】:2014-05-20 18:35:41
【问题描述】:

我认为 mocha 和 async/sequelize 有问题。

我有一个表单,允许用户输入他的伪密码和密码并使用它进行一些异步工作。它工作得很好。但我想为我的所有应用程序编写单元测试。

当我为这部分编写测试时,它不起作用,因为 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 测试中使用 sequelize 初始化/连接到数据库?
  • 是的,我按照此处未提及的要求执行此操作,并且连接正常。但我想我发现了什么不起作用,这是因为我没有将异步功能用于不同的测试。既然我已经这样做了,我可以肯定它更有效。我必须测试它是否真的可以正常工作

标签: node.js asynchronous mocha.js sequelize.js


【解决方案1】:

我发现你写的单元测试至少有一个问题:

它作为同步测试运行。

要在 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 文档:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2023-04-11
    • 2015-06-14
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多