How do I stub out Meteor.call in Velocity?


#1

How would I go about stubbing out specific remote method calls, i.e. Meteor.call or Meteor.apply in package testing? I’m using mocha, chai, and sinon with Velocity.

If I stub out Meteor.call, it breaks Velocity since the test runner uses remote methods itself. This happens in the terminal (meteor test-packages --velocity) and the HTML reporter (meteor test-packages --driver-package respondly:test-reporter). The tests will report as passing but nothing’s actually happening.

This may be as much of a Sinon question as a Meter & Velocity one, but I can’t figure out any way to stub a method only when given certain arguments, otherwise let the function run like normal.


#2

You could try monkey patching the method and conditionally bailing on it depending on the arguments. So something like:

Meteor.__call = Meteor.call;
Meteor.call = function(name) {
   
    // check the params here and do something, maybe even assert here
    if (name === 'aSpecificMethod') return;

    return Meteor.__call.apply(this, arguments);
}

#3

I use following helper (in coffeescript)

# test_helper.coffee:
_original = Meteor.call
@stubMeteorMethod = (methodName, error, ret) ->
  s = Meteor.call
  spy = sinon.spy()
  Meteor.call = () =>
    if arguments[0] == methodName
      lastArg = _.last(arguments)
      spy()
      if typeof lastArg == 'function'
        lastArg(error, ret)
    else
      s.apply(@, arguments)
  return spy

@restoreMethodStubs = () ->
  Meteor.call = _original

You can use it:

...
beforeEach ->
  @methodStub = stubMeteorMethod("duplicateProject", null, "newProjectId")
it "does sth" ->
    Meteor.call("duplicateProject", (error, success) -> success == "newProjectId")
    expect(@methodStub.called).to.equal true
afterEach ->
  restoreMethodStubs()

#4

I’ve just published this as an external package https://atmospherejs.com/hausor/test-helpers


#5

Awesome! I’ll definitely use that the next time I’m writing tests