2017-02-25 60 views
2

我想使用chaiHttp編寫測試nodejs-powered API。下面是我的測試代碼:chaiHttp忽略端口

const mongoose = require("mongoose"); 
const Products = require('../models/product'); 

const chai = require('chai'); 
const chaiHttp = require('chai-http'); 
const server = require('../server'); 
const should = chai.should(); 
const assert = chai.assert; 

chai.use(chaiHttp); 

describe('Products',() => { 
    beforeEach(done => {Products.remove({}, done);}); 

    describe('/GET products',() => { 
    it('it should GET all the products', (done) => { 
     //chai.request(server) 
     chai.request("http://127.0.0.1:3000") 
      .get('api/products') 
      .end((err, res) => { 
      if (err) { 
       assert(false, 'err response: ' + err); 
       return; 
      } 
      res.should.have.status(200); 
      res.body.should.be.a('array'); 
      res.body.length.should.be.eql(0); 
      done(); 
      }); 
    }); 
    }); 
}); 

server.js:

const express = require('express'); 
const mongoose = require('mongoose'); 
const bodyParser = require('body-parser'); 

console.log("NODE_ENV: " + process.env.NODE_ENV); 

const MONGO_URL = 'mongodb://mongodb:27017/maindb'; 
const API_PORT = process.env.PORT || '3000'; 

mongoose.connect(MONGO_URL); 
const dbConnection = mongoose.connection; 

dbConnection.on('error', err => console.log('connection error:', err.message)); 
dbConnection.once('open',() => console.log("Connected to DB!")); 

// Express 
const app = express(); 

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

// Routes 
app.use('/api', require('./routes/api')); 

// Start server 
app.listen(API_PORT,() => { 
    console.log("Server started on port: " + API_PORT); 
    app.emit('started'); 
}); 

module.exports = app; 

當我運行測試,我得到的錯誤:

api_1  | NODE_ENV: test 
api_1  | 
api_1  | 
api_1  | Server started on port: 3000 
mongodb_1 | 2017-02-25T13:22:57.693+0000 I NETWORK [thread1] connection accepted from 172.18.0.3:44252 #1 (1 connection now open) 
api_1  | Products 
mongodb_1 | 2017-02-25T13:22:57.706+0000 I NETWORK [conn1] received client metadata from 172.18.0.3:44252 conn1: { driver: { name: "nodejs", version: "2.2.22" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.8-moby" }, platform: "Node.js v7.4.0, LE, mongodb-core: 2.1.7" } 
api_1  |  /GET products 
api_1  | Connected to DB! 
api_1  |  1) it should GET all the products 
api_1  | double callback! 
api_1  | 
api_1  | 
api_1  | 0 passing (65ms) 
api_1  | 1 failing 
api_1  | 
api_1  | 1) Products /GET products it should GET all the products: 
api_1  |  Uncaught AssertionError: err response: Error: connect ECONNREFUSED 127.0.0.1:80 
api_1  |  at chai.request.get.end (test/test-product.js:22:15) 
api_1  |  at Test.Request.callback (node_modules/superagent/lib/node/index.js:615:12) 
api_1  |  at ClientRequest.<anonymous> (node_modules/superagent/lib/node/index.js:567:10) 
api_1  |  at Socket.socketErrorListener (_http_client.js:309:9) 
api_1  |  at emitErrorNT (net.js:1281:8) 
api_1  |  at _combinedTickCallback (internal/process/next_tick.js:74:11) 
api_1  |  at process._tickCallback (internal/process/next_tick.js:98:9) 

我花了好幾天努力要弄清楚爲什麼chaiHttp請求「127.0.0.1:80」,儘管我明顯在URL中指定端口爲「3000」。

如果我將此行更改爲「chai.request(」http://127.0.0.1:3000「)」更改爲「chai.request(server)」,我會得到相同的結果。

+0

我覺得你的蒙戈網址是錯誤試試這個:'的mongodb://本地主機:27017/maindb' – cubbuk

+0

「的MongoDB」是主機名由docker-compose分配給MongoDB主機。此外根據日誌數據庫連接是好的。 – Evgeny

+0

然後你可以用「/」開始你的測試路徑:'chai.request(「http://127.0.0.1:3000」).get('/ api/products')'。我已複製並測試您的案例,並且由於缺少'/' – cubbuk

回答

3

好像你錯過了你的測試請求的領先/

chai.request("http://127.0.0.1:3000").get('/api/products') // /api/products instead of api/products