node.js - Express.js Mocha testing having issues connecting to test db -
i trying test suite run within express.js app, first express.js app first time working mocha , unsure how set up.
here 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', -> '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 having are... 1) in test suite can't connect test db (in config file); 2) getting error message when trying post
1) post /users/:id/activities should return created document: typeerror: object #<request> has no method 'post'
can point me in right direction on how running properly?
the error getting trying connect mongodb error! error: trying open unclosed connection.
i running mocha suite running 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 connect database @ ' + 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;
first, once have superagent pointing app variable holds routes, middlewares , on, there no need specify url in methods provided superagent. need provide route, so:
request(app) .post('/users/123/activities') .end(function (err, response) { });
second, use before
statement provided mocha instead of beforeeach
, second 1 try connect mongo on every unit test have.
for first error
request has no method 'post'
make sure have installed superagent , can find on node_modules
folder.
hope helps!
Comments
Post a Comment