Alex Rivera | Logout

How do i test my express app with mocha?

Asked 2012-01-12T08:27:33.047
67

I've just added shouldjs and mocha to my express app for testing, but I'm wondering how to test my application. I would like to do it like this:

app = require '../app'
routes = require '../src/routes'

describe 'routes', ->
  describe '#show_create_user_screen', ->
    it 'should be a function', ->
      routes.show_create_user_screen.should.be.a.function
    it 'should return something cool', ->
      routes.show_create_user_screen().should.be.an.object

Of course, the last test in that test-suite just tells med that the res.render function (called within show_create_user_screen) is undefined, probably becouse the server is not running and the config has not been done. So I wonder how other people set up their tests?

Edit
Report

1 Answer

7

mocha comes with before, beforeEach, after, and afterEach for bdd testing. In this case you should use before in your describe call.

describe 'routes' ->
  before (done) ->
    app.listen(3000)
    app.on('connection', done)
answered 2012-01-12T16:09:53.317

Your Answer