用Mocha调用和测试流星方法

Calling and Testing Meteor Methods with Mocha

本文关键字:流星 方法 测试 Mocha 调用      更新时间:2023-09-26

我正在寻找一个解决方案来测试流星方法与摩卡。我正在使用Velocity和Mocha包。

这是我想测试的一个示例方法。

Meteor.methods({
  addPoints: function(userId, points) {
    return Players.update(userId, { $inc: { score: +points } });
  }
});

这是我使用node调用它的方式,我想用参数调用方法并断言在这种情况下,它返回1表示更新mongo文档

if (!(typeof MochaWeb === 'undefined')){
  MochaWeb.testOnly(function(){
    describe("Meteor Method: Upldating a player", function(){
      // define the handler as the method we are testing
      // May be this needs to be a before each.
      var handler = Meteor.call("addPoints");
      var userId = "1";
      var points = 5;
      describe("Updating a player", function() {
        it("Should Add a point", function(done){
          handler(userId, points, function() {
            assert(handler.calledOnce);
            // a way here of asserting the callback is what we expect,
            // in this case we expect a return of 1
            done();
          });
        });
      });
    });
  });
}

谢谢

假设您的测试在服务器上运行,您应该避免向方法调用发送回调。这样,Meteor方法将"同步"运行(在光纤的意义上)。

我将把描述部分重写如下:

describe('Updating a player', function () {
  beforeEach(function() {
    handler(userId, points)
  })
  it('Should Add a point', function () {
    assert(handler.calledOnce)
  })
})