簡體   English   中英

使用sequelize,mocha和async進行單元測試:Sequelize不會回叫

[英]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文檔的一部分:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM