dennismonsewicz
dennismonsewicz

Reputation: 25552

Express.js Mocha testing having issues connecting to test db

I am trying to get my test suite to run within my express.js app, but this is my first express.js app as well as my first time working with mocha and I am unsure how to set everything up.

Here is my test

should = require 'should'
assert = require 'assert'
expect = require 'expect'
request = require 'superagent'
mongoose = require 'mongoose'
config = require '../../config/config'


describe "POST", ->
  app = require('../../app')

  beforeEach (done)->
    mongoose.connect config.db, (err) ->
      if err
        console.log "Error! #{err}"

      done()

  describe '/users/:id/activities', ->
    it 'should return created document', (done) ->
      request(app).post('http://localhost:3000/users/1/activities').send(
        description: "hello world"
        tags: "foo, bar"
      ).set('Content-Type','application/json')
      .end (e, res) ->
        console.log(res.body.description)
        expect(res.body.description).should.equal('hello world')
        done()

A couple of issues I am having are... 1) In the test suite it can't connect to my test db (in my config file); 2) I am getting this error message when trying to post

1) POST /users/:id/activities should return created document:
     TypeError: Object #<Request> has no method 'post'

Can someone point me in the right direction on how to get everything running properly?

The error I am getting back from trying to connect to the MongoDB is Error! Error: Trying to open unclosed connection.

I am running the mocha suite by running this command

NODE_ENV=test mocha test/controllers/activities.test.coffee --compilers coffee:coffee-script/register -R spec

Edit

activities.coffee (routes)

express  = require 'express'
router = express.Router()
mongoose = require 'mongoose'
Activity  = mongoose.model 'Activity'

module.exports = (app) ->
  app.use '/', router

router.post '/users/:id/activities', (req, res, next) ->
  activity = Activity(
    user_id: req.params.id
    description: req.body.description,
    tags: req.body.tags
  )

  activity.save (err)->
    if not err
      res.json(activity)
    else
      res.json({ error: err })

router.get '/users/:id/activities/:activity_id', (req, res, next) ->
  Activity.findById req.params.activity_id, (err, activity) ->
    if not err
      res.json(activity)
    else
      res.json({ error: err })

app.js

require('coffee-script/register');

var express = require('express'),
  config = require('./config/config'),
  fs = require('fs'),
  mongoose = require('mongoose');

mongoose.connect(config.db);
var db = mongoose.connection;
db.on('error', function () {
  throw new Error('unable to connect to database at ' + config.db);
});

var modelsPath = __dirname + '/app/models';
fs.readdirSync(modelsPath).forEach(function (file) {
  if (/\.coffee$/.test(file)) {
    require(modelsPath + '/' + file);
  }
});
var app = express();

require('./config/express')(app, config);

app.listen(config.port);

exports.app = app;

Upvotes: 0

Views: 922

Answers (1)

Danillo Corvalan
Danillo Corvalan

Reputation: 713

First, once you have your superagent pointing to you app variable that holds you routes, middlewares and so on, there is no need to specify the url in the methods provided by superagent. You just need to provide the route, just like so:

request(app)
  .post('/users/123/activities')
  .end(function (err, response) {
  });

Second, use the before statement provided by mocha instead of beforeEach, the second one will try to connect to mongo on every unit test you have.

For the first error

request has no method 'post'

Make sure you have installed superagent and you can find it on your node_modules folder.

Hope that helps!

Upvotes: 1

Related Questions