摩卡测试不等待发布/订阅

Car*_*aza 1 javascript testing velocity mocha.js meteor

使用Mocha + Velocity(0.5.3)进行Meteor客户端集成测试.我们假设我安装了自动发布包.

问题

如果从服务器插入MongoDB上的文档,则客户端mocha测试将不会等待订阅同步,从而导致断言失败.

代码示例

服务器端Accounts.onCreateUser钩子:

Accounts.onCreateUser(function (options, user) {
  Profiles.insert({
    'userId': user._id,
    'profileName': options.username
  });

  return user;
});
Run Code Online (Sandbox Code Playgroud)

客户端摩卡测试:

beforeEach(function (done) {
  Accounts.createUser({
    'username'  : 'cucumber',
    'email'     : 'cucumber@cucumber.com',
    'password'  : 'cucumber' //encrypted automatically
  });

  Meteor.loginWithPassword('cucumber@cucumber.com', 'cucumber');
  Meteor.flush();
  done();
});

describe("Profile", function () {

  it("is created already when user sign up", function(){
    chai.assert.equal(Profiles.find({}).count(), 1);
  });

});
Run Code Online (Sandbox Code Playgroud)

我怎么能让Mocha等待我的个人资料文件进入客户端,避免Mocha超时(从服务器创建)?

小智 5

您可以反应等待文档.Mocha有超时,因此如果没有创建文档,它会在一段时间后自动停止.

it("is created already when user signs up", function(done){
  Tracker.autorun(function (computation) {
    var doc = Profiles.findOne({userId: Meteor.userId()});
    if (doc) {
      computation.stop();
      chai.assert.propertyVal(doc, 'profileName', 'cucumber');
      done();
    }
  });
});
Run Code Online (Sandbox Code Playgroud)