Express.js Mocha测试连接到测试数据库时遇到问题

时间:2014-09-10 14:11:47

标签: node.js mongodb express mocha

我想让我的测试套件在我的express.js应用程序中运行,但这是我的第一个express.js应用程序以及我第一次使用mocha而且我不确定如何设置所有内容。

这是我的测试

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()

我遇到的一些问题是...... 1)在测试套件中,它无法连接到我的测试数据库(在我的配置文件中); 2)尝试post

时收到此错误消息
1) POST /users/:id/activities should return created document:
     TypeError: Object #<Request> has no method 'post'

有人能指出我如何使一切正常运转的正确方向吗?

我尝试连接到MongoDB时遇到的错误是Error! Error: Trying to open unclosed connection.

我通过运行此命令来运行mocha套件

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

修改

activities.coffee(路线)

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;

1 个答案:

答案 0 :(得分:1)

首先,一旦你的superagent指向你的app变量,它包含路由,中间件等,就不需要在superagent提供的方法中指定url。你只需要提供路线,就像这样:

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

其次,使用mocha提供的before语句而不是beforeEach,第二个语句将尝试在每个单元测试中连接到mongo。

第一个错误

request has no method 'post'

确保已安装superagent,您可以在node_modules文件夹中找到它。

希望有所帮助!