I had a look again today, and managed to get around the problem I encountered by making one of the variables in my `
before (done) ->` callback a global variable. This solved it for me.
It was actually a Sails v0.10.0-rc5 server that I was testing with mocha and coffeescript. Here's the configuration that got it working for me, in case it helps anyone. Without setting the app object to be global my tests couldn't access it.
# tests/test_init.coffee
Sails = require 'sails/lib/app'
global.app = Sails()
before (done) ->
# Lift app and store the app reference
app.lift
# Basic options you should pretty much always use for tests:
# turn down the log level so we can view the test results
log:
level: "error"
port: 5000
models:
connection: 'testLocalDiskDb'
hooks:
grunt: false
, done
after (done) ->
try
fs.unlinkSync '../../.tmp/testLocalDiskDb.db' # temp db to be deleted
catch error
console.log 'DB not yet created therefore - not deleted'
app.lower done
-----------------------------
-----------------------------
# tests/test_example.coffee
request = require 'supertest'
fs = require 'fs'
should = require('chai').should()
describe 'when lifting Server', (done) ->
it 'should respond with a 200 response code', (done) ->
# basic test of api
request(app.ws.server)
.get('/')
.set('Content-Type', 'application/json')
.end (err, res) ->
should.exist(res.status)
res.status.should.equal 200
done()