摩卡试验不等待发布/订阅(Mocha test is not waiting for Publica

2019-10-22 21:50发布

使用流星客户端集成测试摩卡+速度(0.5.3)。 让我们假设我已经安装包自动发布

问题

如果MongoDB的文档从服务器插入时,客户端摩卡测试不会等待订阅同步,导致断言失败。

代码示例

服务器端Accounts.onCreateUser钩:

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

  return user;
});

客户端摩卡测试:

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

});

我如何才能让摩卡等待我的个人资料文件进行的方式向客户避免了摩卡超时(从服务器创建的)?

Answer 1:

您可以被动等待的文件。 摩卡有超时,所以如果不创建的文档会自动在一段时间后停止。

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


Answer 2:

Accounts.createUser有一个可选的回调,只需拨打这个回调中完成的功能,如:

beforeEach(function (done) {
    Accounts.createUser({
        'username'  : 'cucumber',
        'email'     : 'cucumber@cucumber.com',
        'password'  : 'cucumber' //encrypted automatically
    }, () => {
        // Automatically logs you in
        done();
    });
});
describe('Profile', function () {
    it('is created already when user sign up', function () {
        chai.assert.equal(Profiles.find({}).count(), 1);
    });
});


文章来源: Mocha test is not waiting for Publication/Subscription