Mongo客户端在cucumberjs测试中不返回数据



我已经用几种不同的方式把它拆开了。 查找在删除后发生,并且查找永远不会找到任何内容。 如果我注释掉this.accounts.remove...,则查找有效。 如果我将删除行留在那里,它就不会。 我对Cucumberjs,mongo客户端和节点的理解表明,该查找应该有效。

我什至尝试将删除/查找序列移动到它自己的文件中,它在那里工作。 似乎只有当我在黄瓜中运行它时,序列才会失败。 我怀疑是因为黄瓜加载文件的方式,但我不确定。

有人可以帮助我弄清楚如何让它工作吗?

世界.js:

var db = new Db('FlashCards', new Server('localhost', 27017));
db.open(function(err, opened) {
  if (err) {
    console.log("error opening: ", err);
    done(err);
  }
 db = opened;
});
var {
  defineSupportCode
} = require('cucumber');
function CustomWorld() {
  this.db = db;
 this.accounts = db.collection('accounts');

钩子.js:

Before(function(result, done) {
  //comment this out, and leave a done(), it works!!!!
  this.accounts.remove(function(error, result){
    if( error) {
      console.log("Error cleaning the database: ", error);
      done(error);
    }
    done();
  })
});

user_steps.js:

Then('I will be registered', function(done) {
  let world = this;
  this.accounts.find({
    username: world.user.username
  }).toArray(
    function(err, accounts) {
      if (err) {
        console.log("Error retrieveing data: ", err);
        done(err);
      }
      console.log("Accounts found: ", accounts);
      expect(accounts).to.be.ok;
      expect(accounts.length).to.be.equal(1);
      done();
   });
});

创新:

cucumber-js --compiler es6:babel-core/register

您缺少要在 remove 方法中删除的项目。我假设要删除的项目是

this.accounts.remove(function(error, result({

您缺少一个用于删除方法的参数。该参数是要删除的查询。我假设,删除查询是{用户名:world.user.username}

var qry={用户名: world.user.username};请尝试以下操作:

Before(function(result, done) { //comment this out, and leave a done(), it works!!!!
    var qry={username: world.user.username};
     this.accounts.remove(qry, function(error, result){ 
     if( error) { 
               console.log("Error cleaning the database: ", error);
               done(error);
       } 
      done(); 
       }) });

最新更新