我正在尝试使用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请求“127.0.0.1:80”,尽管我显然在URL中将端口指定为“3000”。
如果将“chai.request(“http://127.0.0.1:3000”)”行更改为“chai.request(server)”,这在这里更加一致,那么我将得到相同的结果。
最佳答案
似乎您错过了测试请求中的前导/
:
chai.request("http://127.0.0.1:3000").get('/api/products') // /api/products instead of api/products
关于javascript - chaiHttp忽略端口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42456742/