Meteor 1.3 full-app method tests - stubs on both client and server

Problem:

Stubbed functions that are called by a Meteor method behave differently on the client and server.

This test passes on the server.

But on the client, the stub does not work and I get an error thrown by funcOne, which means that funcOne did not return true.

Using:

*Meteor 1.3-rc.4
*avital:mocha
*practicalmeteor:chai
*practicalmeteor:sinon

Code:

describe 'items', ->
  describe 'addItem', ->

    item = null

    beforeEach ->
      Meteor.call 'emptyCollection', 'Items'
      
      item = new Item({
        name : "Thing"
        text: "Lets get this test working"
      })

      sinon.stub utils, 'funcOne', -> true

    afterEach ->
      utils.funcOne.restore()

    it 'should add an item to DB', ->
      Meteor.call('addItem', item)
      itemInDb = Items.findOne()
      expect(itemInDb).to.be.a('object')
      expect(itemInDb).to.have.property('_id')

Tried:

I have tried variations of restoring the stubbed method only on the server or not restoring at all, which does give me different errors, but still no joy.

I have looked into load order issues:

  • my tests are in the same folder as the methods - root/methods/items.coffee
  • App.utils file is in root/lib/
  • I am currently not using imports/exports folders

Help?

Anybody have any ideas… Thanks.

1 Like

Update

I solved the problem by testing Meteor.methods only on the server by wrapping them in a if Meteor.isServer. I figure that because the server is the source of truth that should be good enough. Let me know if you have any more thoughts on how best to test Meteor.methods.

1 Like