【问题标题】:Error with running the Mocha test cases using chai-http for node services使用 chai-http 为节点服务运行 Mocha 测试用例时出错
【发布时间】:2021-06-14 13:52:42
【问题描述】:

我试图掌握使用 mocha 和 chai-http 编写测试用例的基本技巧,我编写了如下测试用例

let chai = require('chai');
let chaiHttp = require('chai-http');

const should = chai.should;
const expect = chai.expect;
const server = "http://127.0.0.1:3000"

chai.use(chaiHttp);

describe('Create Login and Register', () => {
    it('should login using credentials', (done) => {
        chai.request(server)
            .get('/register')
            .send()
            .then((res: any) => {
                res.should.have.status(200);
                done();
            }).catch((err: any) => { done(err) })
    })

})

我要测试的服务如下

const express = require('express');
const app = express();

app.get('/register', function (req, res) {
    res.json({
        'state': true,
        'msg': 'Register endpoint',
        'data': {
            'username': 'Swarup',
            'email': 'abc@gmail.com',
            'password': 'P@1234',
            'fullName': 'Swarup Default'
        }
    });
});

app.listen(3000, () => { console.log('started') })

module.exports = app;

但是当我运行测试用例时,出现如下错误

1 failing

  1) Create Login and Register
       should login using credentials:
     Error: connect ECONNREFUSED 127.0.0.1:3000
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)

我错过了什么或做错了什么?

【问题讨论】:

    标签: node.js express mocha.js chai chai-http


    【解决方案1】:

    您没有启动 HTTP 服务器。您应该在 before 钩子中启动 HTTP 服务器并在 after 钩子中拆除它。

    此外,您可以使用require.main === module 条件让您的模块NOT在要求时执行条件块中的代码。因为我们会在我们的测试文件中require('./app'),所以我们不想在要求时启动 HTTP 服务器。

    例如

    app.js:

    const express = require('express');
    const app = express();
    
    app.get('/register', function (req, res) {
      res.json({
        state: true,
        msg: 'Register endpoint',
        data: {
          username: 'Swarup',
          email: 'abc@gmail.com',
          password: 'P@1234',
          fullName: 'Swarup Default',
        },
      });
    });
    
    if (require.main === module) {
      app.listen(3000, () => {
        console.log('started');
      });
    }
    
    module.exports = app;
    

    app.test.js:

    let chai = require('chai');
    let chaiHttp = require('chai-http');
    let app = require('./app');
    
    const expect = chai.expect;
    const endpoint = 'http://127.0.0.1:3000';
    chai.use(chaiHttp);
    
    describe('Create Login and Register', () => {
      let server;
      before(() => {
        server = app.listen(3000, () => {
          console.log('started for testing');
        });
      });
      after(() => {
        server.close();
      });
      it('should login using credentials', (done) => {
        chai
          .request(endpoint)
          .get('/register')
          .send()
          .then((res) => {
            expect(res).to.have.status(200);
            done();
          })
          .catch((err) => {
            done(err);
          });
      });
    });
    

    测试结果:

      Create Login and Register
    started for testing
        ✓ should login using credentials
    
    
      1 passing (18ms)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-15
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多