我正在尝试使用chaiHttp为nodejs驱动的API编写测试。这是我的测试代码:
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要求&#34; 127.0.0.1:80&#34;尽管我明显将端口指定为&#34; 3000&#34;在URL。
如果我更改了行&#34; chai.request(&#34; http://127.0.0.1:3000&#34;)&#34; to&#34; chai.request(服务器)&#34;这里更加一致,我得到了相同的结果。
答案 0 :(得分:4)
好像你错过了测试请求中的前导/
:
chai.request("http://127.0.0.1:3000").get('/api/products') // /api/products instead of api/products