[英]Error with running the Mocha test cases using chai-http for node services
I was trying to get a basic hang of writing the test cases using mocha and chai-http,I had written the test case as below我试图掌握使用 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) })
})
})
and the service that i'm trying to test is as below我要测试的服务如下
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;
but when i run the test case i get an error as given below但是当我运行测试用例时,出现如下错误
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)
what is that i'm missing or doing wrong?我错过了什么或做错了什么?
You did not start the HTTP server.您没有启动 HTTP 服务器。 You should start the HTTP server in the before
hook and teardown it in the after
hook.您应该before
钩子中启动 HTTP 服务器,并在after
钩子中将其拆除。
Besides, you can have your module NOT execute the code in the conditional block at requiring by using require.main === module
condition.此外,您可以使用require.main === module
条件让您的模块不执行条件块中的代码。 Because we will require('./app')
in our test file, we don't want to start the HTTP server at requiring.因为我们将在我们的测试文件中使用require('./app')
,所以我们不想在要求时启动 HTTP 服务器。
Eg例如
app.js
: 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
: 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);
});
});
});
test result:测试结果:
Create Login and Register
started for testing
✓ should login using credentials
1 passing (18ms)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.