使用Mocha& amp;节点测试柴 - >错误.. TypeError:app.address不是函数

时间:2017-02-28 17:21:30

标签: javascript node.js express mocha chai

尝试在节点

中测试基本GET路由时出现以下错误
TypeError: app.address is not a function

我正在检索我要测试的应用程序代码但我在代码中没有看到任何对“地址”错误的引用,因此我不知道要修复什么。

任何人都有任何建议吗?

以下是我的单元测试

let chai = require('chai');
let chaiHttp = require('chai-http');
let app = require('../src/app');
let should = chai.should();

chai.use(chaiHttp);
describe('/POST getRating', () => {
      it('it should not POST a book without pages field', (done) => {
        chai.request(app)
            .get('/')
            // .send(testData1)
            .end((err, res) => {
                console.log('ERROR', err);
                res.should.have.status(200);
                res.body.should.be.a('string');
              done();
            });
      });

  });

以下是我的app.js代码

import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import config from  './config';
import http from 'http'
mongoose.Promise = Promise;
import rating from './components';

const cors = config.cors
const mongouri = config.mongoURI;
mongoose.connect(mongouri);

const app = express();

app.use(cors.cors(cors.origins));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));


app.get("/", (req, res) => res.json({message: "Welcome to our Bookstore!"}));

app.use('/api/rating', rating);

const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

const server = http.createServer(app);

server.listen(port);
server.on('listening', onListening);

function normalizePort(val) {
  let port = parseInt(val, 10);

  if (isNaN(port)) {
    return val;
  }

  if (port >= 0) {
    return port;
  }

  return false;
}

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  console.log('Listening on ' + bind);
}

export default app;

1 个答案:

答案 0 :(得分:7)

由于蒸腾,这可能是一个问题。试试:

protobuf