0

我正在尝试编写一个自动化脚本来测试我的一些 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)与我在第一步中运行的函数的完成相关联?

谢谢!

::::更新::::

如果有人在乎,我必须扩展它并调用更多方法来模拟用户流程,这就是它最终的样子..如果有人想改进这一点,请做

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.');   
});
4

1 回答 1

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;
},
于 2013-03-27T17:49:57.130 回答