Node mocha API 使用 SocketIO 和 Express 进行测试
Node mocha API testing with SocketIO and Express
我正在做一个项目,我需要为 API 提供 HTTP 请求并处理用户通过套接字相互通信(我正在使用 Socket.io 用于此目的) .我的server.js文件的代码如下:
let initHttpServer = () => {
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(require('./routes'));
app.get('/config.js', function(req,res) { res.write("var ROOT_URL='"+process.env.ROOT_URL+"'" + '\n'); res.end(); });
http.listen(port, function() {
console.log('express listening on port' + port);
console.log('a user connected');
});
return app;
}
.
.
.
conn.once('open', function() {
initHttpServer();
});
module.exports = initHttpServer;
我还有一个 io.on('connect'...) 函数,但为了简洁起见,我没有在这里发布(至少现在)。
当我使用 Postman 进行测试时它工作正常,但是我在使用 mocha 和 Chai 测试 HTTP 端点时遇到了问题。我现在要测试的代码如下:
chai.use(chaiHttp);
it('should get votes', function(done) { // <= Pass in done callback
chai.request('http://localhost:3000')
.get('/vote')
.then(function(res) {
res.should.have.status(200);
})
.catch(function(err) {
throw err;
});
});
当我 运行 npm test
时,出现以下错误:
Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我试过将我的测试函数放在 try/catch 块中,如下所示:
it('should return all votes on /votes', function(done) {
try{
chai.request('http://127.0.0.1:3000')
.get('/vote')
.end(function(req,res) {
res.should.have.status(787432189700);
done();
});
--> done();
} catch(error) {
done(error);
}
});
如果我完成了();调用我用箭头 (-->) 指示的,我得到上述错误,但如果我保持它只是 returns 成功而没有测试任何东西。我假设这是一个异步调用,所以 done() 在测试完成之前被调用。因此,我不知道如何进行。我应该如何测试 API 端点?
谢谢!
第一个:
您的 try/catch 块将不起作用,您正在进行异步调用。
第二
最有可能的是这一行:res.should.have.status(787432189700);
有一个错误,所以done没有被执行。
尝试这样的事情:
let chai = require('chai')
, chaiHttp = require('chai-http');
const expect = require('chai').expect;
chai.use(chaiHttp);
describe('Some test', () => {
it('Lets see', (done) => {
chai.request('http://localhost:3000')
.get('/vote')
.end(function (err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});
});
});
请注意回调函数的第一个参数是错误。
我正在做一个项目,我需要为 API 提供 HTTP 请求并处理用户通过套接字相互通信(我正在使用 Socket.io 用于此目的) .我的server.js文件的代码如下:
let initHttpServer = () => {
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(require('./routes'));
app.get('/config.js', function(req,res) { res.write("var ROOT_URL='"+process.env.ROOT_URL+"'" + '\n'); res.end(); });
http.listen(port, function() {
console.log('express listening on port' + port);
console.log('a user connected');
});
return app;
}
.
.
.
conn.once('open', function() {
initHttpServer();
});
module.exports = initHttpServer;
我还有一个 io.on('connect'...) 函数,但为了简洁起见,我没有在这里发布(至少现在)。 当我使用 Postman 进行测试时它工作正常,但是我在使用 mocha 和 Chai 测试 HTTP 端点时遇到了问题。我现在要测试的代码如下:
chai.use(chaiHttp);
it('should get votes', function(done) { // <= Pass in done callback
chai.request('http://localhost:3000')
.get('/vote')
.then(function(res) {
res.should.have.status(200);
})
.catch(function(err) {
throw err;
});
});
当我 运行 npm test
时,出现以下错误:
Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我试过将我的测试函数放在 try/catch 块中,如下所示:
it('should return all votes on /votes', function(done) {
try{
chai.request('http://127.0.0.1:3000')
.get('/vote')
.end(function(req,res) {
res.should.have.status(787432189700);
done();
});
--> done();
} catch(error) {
done(error);
}
});
如果我完成了();调用我用箭头 (-->) 指示的,我得到上述错误,但如果我保持它只是 returns 成功而没有测试任何东西。我假设这是一个异步调用,所以 done() 在测试完成之前被调用。因此,我不知道如何进行。我应该如何测试 API 端点?
谢谢!
第一个:
您的 try/catch 块将不起作用,您正在进行异步调用。
第二
最有可能的是这一行:res.should.have.status(787432189700);
有一个错误,所以done没有被执行。
尝试这样的事情:
let chai = require('chai')
, chaiHttp = require('chai-http');
const expect = require('chai').expect;
chai.use(chaiHttp);
describe('Some test', () => {
it('Lets see', (done) => {
chai.request('http://localhost:3000')
.get('/vote')
.end(function (err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});
});
});
请注意回调函数的第一个参数是错误。