chrishillonline
chrishillonline

Reputation: 25

Calling and Testing Meteor Methods with Mocha

I am looking for a solution to test Meteor Methods with mocha. I am using Velocity and the Mocha package.

This is an example method I am trying to test.

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

This is, in a round about way how I would call it using node, I want to call the methods with arguments and assert that in this case, it returns 1 for updating the mongo document

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

    });
  });
}

Thanks

Upvotes: 0

Views: 391

Answers (1)

Tomas Romero
Tomas Romero

Reputation: 8698

Assuming your tests run on the server, you should avoid sending a callback to the method call. This way, the Meteor method will run "synchronously" (in the fibers sense).

I would re-write the describe section as follows:

describe('Updating a player', function () {

  beforeEach(function() {
    handler(userId, points)
  })

  it('Should Add a point', function () {
    assert(handler.calledOnce)
  })

})

Upvotes: 0

Related Questions