Skip to content

Instantly share code, notes, and snippets.

@jkresner
Created April 13, 2013 08:44
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 jkresner/5377631 to your computer and use it in GitHub Desktop.
Save jkresner/5377631 to your computer and use it in GitHub Desktop.
TDD nodejs, mocha, mongo, mongoose with a test database
mongoose = require 'mongoose'
wipeDbAfterTests = true
describe "Your test suite", ->
# connect to our mongo test database, note how done is passed to mongoose connect
before (done) ->
mongoose.connect "mongodb://localhost/airpair_test", done
# your test
it "your test", (done) ->
# some call that has mongoose in there somewhere, you'll
# need to call done at some point as mongoose is async
done()
# close our mongoose connection, other wise your next test suite will fail
# saying there is already an open connection
after (done) ->
if ! wipeDbAfterTests
mongoose.connection.close done
else
mongoose.connection.db.executeDbCommand { dropDatabase:1 }, (err, result) ->
mongoose.connection.close done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment