javascript - chaiHttp 忽略端口

标签 javascript node.js chai

我正在尝试使用 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();
          });
    });
  });
});

服务器.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)

尽管我在 URL 中显然将端口指定为“3000”,但我花了几天时间试图弄清楚为什么 chaiHttp 请求“127.0.0.1:80”。

如果我将行“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/

相关文章:

javascript - 样式建议下拉菜单

javascript - 如何测试 Promise.all() 上的 catch block ?

node.js - 将 ChaiHttp 与 beforeEach 或 before 方法一起使用

javascript - 在 JavaScript、Mocha 和 Chai 中测试类函数

javascript - IE8 将 function() 分配给属性名称时出错 'function'

javascript - 从 iframe 操作重定向父窗口

javascript - 使用 JS/Jquery 在现有 HTML 元素中添加图像

javascript - 如何检索拆分的值并将它们放入单独的变量中

javascript - 在 Node 服务器上使用 pdf.js

javascript - 在 Javascript 服务器/客户端中共享对象定义