Mocha测试不在等待发布/订阅 [英] Mocha test is not waiting for Publication/Subscription

查看:59
本文介绍了Mocha测试不在等待发布/订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Mocha + Velocity(0.5.3)进行Meteor客户端集成测试.假设我已经安装了 autopublish 软件包.

Using Mocha + Velocity (0.5.3) for Meteor client-side integration tests. Let's assume that I have autopublish package installed.

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

If a document on the MongoDB was inserted from the server, the client-side mocha tests will not wait for the subscription to synchronise, causing the assertion to fail.

服务器端Accounts.onCreateUser挂钩:

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

  return user;
});

客户端Mocha测试:

Client-side Mocha Test:

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);
  });

});

问题

如何使 Mocha 等待我的个人资料文档发送到客户端,从而避免Mocha超时(从服务器创建)?

Question

How could I make Mocha wait for my profile document to make its way to the client avoiding the Mocha timeout (created from the server)?

推荐答案

您可以被动地等待文档. Mocha超时,因此如果未创建文档,它将在一段时间后自动停止.

You can reactively wait for the documents. Mocha has a timeout, so it would stop automatically after some time if the documents are not created.

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();
    }
  });
});

这篇关于Mocha测试不在等待发布/订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆