How do I stub out in Velocity?

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

If I stub out, 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.

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

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);
1 Like

I use following helper (in coffeescript)

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

@restoreMethodStubs = () -> = _original

You can use it:

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

I’ve just published this as an external package

1 Like

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