Skip to content

Instantly share code, notes, and snippets.

@nidate
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nidate/0311382784f7895d13a6 to your computer and use it in GitHub Desktop.
Save nidate/0311382784f7895d13a6 to your computer and use it in GitHub Desktop.
Replace method by sinon.stub for testing
sinon = require 'sinon'
MongooseModel = require 'mongoosemodeldefinition'
describe 'when the mongoose document failed to save', ()->
stub = null
before (done)->
origSave = MongooseModel.prototype.save
createCount = 0
stub = sinon.stub MongooseModel.prototype, 'save', (cb)->
createCount += 1
if createCount > 3
return cb new Error 'Cannot create greater than 3 documents'
origSave.apply this, arguments
do done
after (done)->
stub.restore()
do done
it '(write test...)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment