Node / Express.js mocha设置无法正常工作

时间:2014-10-06 20:32:59

标签: node.js express mocha

这是我的测试文件:

var request = require('superagent'),
    expect = require('expect'),
    sinon = require('sinon'),
    app = require("../../../../app");

request = request(app);

describe("HealthCheck", function () {

    describe("/health_check", function () {
        it("should return a 200 status", function (done) {
            request
            .get("http://localhost:3000/health_check")
            .end(function(err, res) {
                expect(res).to.exist;
                expect(res.status).to.equal(200);
                expect(res.body).to.contain('OK');

                return done();
            });
        });
    });
})

我一直遇到的错误是

  1) HealthCheck /health_check should return a 200 status:
     TypeError: Cannot call method 'end' of undefined

无法弄清楚我做错了什么

这是我的app.js:

require('coffee-script/register');

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

var app = express();

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

app.listen(config.port);

exports.app = app;

1 个答案:

答案 0 :(得分:1)

您将应用导出为app
因此,当您require该模块时,您需要require您要导出的功能的名称 - 在这种情况下' app'

所以,你可以改变你的考试

var request = require('superagent'),
    expect = require('expect'),
    sinon = require('sinon'),
    app = require("../../../../app");

request = request(app.app);

或者更改您的app.js以导出单个功能:

更改

exports.app = app;

module.exports = app;

这里有一篇很好的文章:http://openmymind.net/2012/2/3/Node-Require-and-Exports/