为什么地址在我的应用中未定义?

时间:2015-06-15 03:20:47

标签: node.js express jasmine supertest

我有一个简单的快递应用程序:

var express = require('express');
var path = require('path');

var app = express();
exports.app = app;

var index = require('./routes/index');

app.use(express.static(path.join(__dirname,'client/dist/')));

app.get('/', index.get);

function start(){
    var port = process.env.PORT || 8080;

    app.listen(port, function(){
        console.log('app is running on port: ' + port);
    });
};

exports.start = start;

进行集成测试:

var request = require('supertest');

var app = require('../app');

describe('GET /', function(){
    it('should repsond with 200', function(done){
        request(app)
        .get('/')
        .expect(200, done.fail);
    });
});

该应用程序运行正常,但运行集成测试,我收到以下错误:

Failures: 
1) GET / should repsond with 200
1.1) TypeError: Object #<Object> has no method 'address'

我做了一些搜索,似乎应用程序没有正确导出,但我似乎无法找出原因。

1 个答案:

答案 0 :(得分:3)

request(app.app)而不是集成测试中的request(app)应修复错误。

var request = require('supertest');
var app = require('../app');

describe('GET /', function(){
  it('should repsond with 200', function(done){
      request(app.app)
      .get('/')
      .expect(200, done.fail);
  });
});