我正在尝试编写一个自动化脚本来测试我的一些api调用,这是一个简单的例子:
async.series([
function(callback){
func.clean_database()
callback(null);
},
function(callback){
func.register_and_auth(users[0])
callback(null);
}
],
function(err, results){
console.log('done');
});
清理数据库看起来很简单:
clean_database: function () {
var Projects = mongoose.model("Projects", new mongoose.Schema({}));
Projects.collection.remove(function (err, results) { console.log("Emptied: Projects"); });
return true;
},
问题是register_and_auth(users [0])在清理数据库完成之前运行..因此告诉我用户已经注册。我的印象是async.series将串行运行,即一个接一个。
如何才能使回调(null)与我在第一步中运行的函数的完成相关联?
谢谢!
:::: UPDATE ::::
如果有人关心,我必须扩展这个并调用更多方法来模仿用户流程,这是最终的样子......如果有人想改进这一点,请做
async.series([
func.clean_database, function(one_callback){
func.register_and_auth(users[0], function(callback) {
console.log('-- Starting Create and Upload');
func.auth_and_create_project_and_upload(users[0], data, function(callback) {
console.log('-- Starting Annotations');
func.auth_open_project_add_annotations_to_all_pages(users[0], annotation, function(callback) {
console.log('-- Done with User 0.');
one_callback();
});
});
});
},
function(two_callback) {
func.register_and_auth(users[1], function(callback) {
console.log('-- Starting Create and Upload');
func.auth_and_create_project_and_upload(users[1], data, function(callback) {
console.log('-- Starting Annotations');
func.auth_open_project_add_annotations_to_all_pages(users[1], annotation, function(callback) {
console.log('-- Done with User 1.');
two_callback();
});
});
});
},
],
function(err, results){
console.log('Load test complete for two users.');
});
答案 0 :(得分:1)
您需要传递回调并在任务完成后调用它们
async.series([
func.clean_database,
function(callback){
func.register_and_auth(users[0], callback);
}
],
function(err, results){
console.log('done');
});
clean_database: function (callback) {
var Projects = mongoose.model("Projects", new mongoose.Schema({}));
Projects.collection.remove(function (err, results) {
console.log("Emptied: Projects");
callback(err, results);
});
return true;
},