如何解决如何存根 mocha 测试套件中服务器上使用的函数
在快速服务器 auth.session
上测试端点 /allowUser2
时,我试图存根 app.js
。
//--auth.js--
module.exports.session = (req,res,next) => {
req.user = null;
next();
};
//--app.js--
const express = require('express');
const auth = require('./auth');
const app = express();
app.use(auth.session);
app.get('/allowUser2',(req,res) => {
if (!req.user) return res.status(401).send();
if (req.user.user === 2) return res.status(200).send();
});
app.listen(4001).on('listening',() => {
console.log(`HTTP server listening on port 4001`);
});
module.exports = app;
如果我的测试套件中只有这个测试文件 test1.js
,auth
会成功存根。
//--test1.js--
let app;
const sinon = require('sinon');
const auth = require('../../auth.js');
const chai = require('chai');
const chaiHttp = require('chai-http');
const { expect } = chai;
chai.use(chaiHttp);
let agent;
describe('should allow access',() => {
before(async () => {
// delete require.cache[require.resolve('../../app.js')]; // causes Error: listen EADDRINUSE: address already in use
sinon.stub(auth,'session').callsFake((req,next) => {
req.user = { user: 1 };
next();
});
app = require('../../app.js');
agent = chai.request.agent(app);
});
after(async () => {
auth.session.restore();
});
it('should not allow access',async function () {
const response = await agent.get('/allowUser2');
expect(response.status).to.be.equal(200);
});
});
但是,如果我有多个需要 app.js
的测试文件,那么我就有问题了。如果另一个测试文件中已经需要 app.js
,例如下面的 test2.js
,则当 app.js
中再次需要时,节点不会重新加载 test1.js
。这会导致 app.js
调用旧的 auth.session
函数,而不是新的存根函数。所以用户没有通过身份验证,测试失败。
//--test2.js--
const chai = require('chai');
const chaiHttp = require('chai-http');
const app = require('../../app.js');
const { expect } = chai;
chai.use(chaiHttp);
const agent = chai.request.agent(app);
describe('route /allowUser2',() => {
it("shouldn't allow access",async function () {
const response = await agent.get('/allowUser2');
expect(response.status).to.be.equal(401);
});
});
我尝试使用 app.js
重新加载 delete require.cache[require.resolve('../../app.js')];
。这在使用普通 function 重新加载文件时有效,但是当文件是像 app.js
这样的服务器时,这会导致错误:Error: listen EADDRINUSE: address already in use
。
重新创建:
- 下载Repo
npm i
npm test
如何在服务器上存根函数?
解决方法
更新:建议的解决方案 https://github.com/DashBarkHuss/mocha_stub_server/pull/1
一个问题是您在 app.js 中使用直接方法引用的方式阻止了 Sinon 工作。 https://gist.github.com/corlaez/12382f97b706c964c24c6e70b45a4991
另一个问题(正在使用的地址)是因为每次我们想要获取对应用程序的引用时,我们都试图在同一端口中创建一个服务器。将应用/服务器创建分解为单独的步骤可以缓解该问题。
,一种解决方案是将 app.js
转换为一个函数,该函数在作为参数传入的端口号上启动服务器。然后在需要时随机更改端口。我不喜欢这个选项,因为可能有某种原因将应用程序保留在特定端口上。
app.js
const express = require('express');
const auth = require('./auth');
module.exports = (port) => {
const app = express();
app.use(auth.session);
app.get('/allowUser2',(req,res) => {
if (!req.user) return res.status(401).send();
if (req.user.user === 2) return res.status(200).send();
});
app.listen(port).on('listening',() => {
console.log(`HTTP server listening on port ${port}`);
});
return app;
};
需要时
app = require('../../app.js')((Math.random() * 10000).toString().slice(0,4));
,
我没有导出 app
中的 app.js
,而是导出一个启动服务器并返回服务器实例和应用程序的函数。通过导出服务器实例,我可以关闭服务器。该应用程序需要传递到柴。确保 const app = express();
在这个函数中而不是在它之前,否则它不会重新创建。
const express = require('express');
const auth = require('./auth');
const port = 4000;
module.exports = () => {
const app = express();
app.use(auth.session);
app.get('/allowUser2',res) => {
if (!req.user) return res.status(401).send();
if (req.user.user === 2) return res.status(200).send();
});
app.post('/allowUser2',res) => {
if (!req.user) return res.status(401).send();
if (req.user.user === 2) return res.status(200).send();
});
return {
server: app.listen(port).on('listening',() => {
console.log(`HTTP server listening on port ${port}`);
}),app,};
};
然后在我的测试中,我可以在 before
中启动服务器,并在 两个 测试中在 after
中关闭服务器。
let app;
const sinon = require('sinon');
const auth = require('../../auth.js');
const chai = require('chai');
const chaiHttp = require('chai-http');
const { expect } = chai;
chai.use(chaiHttp);
let server;
describe('route /allowUser2',() => {
before(async () => {
// delete require.cache[require.resolve('../../app.js')]; // causes an error: `Error: listen EADDRINUSE: address already in use`.
sinon.stub(auth,'session').callsFake((req,res,next) => {
req.user = { user: 2 };
next();
});
server = require('../../app.js')();
agent = chai.request.agent(server.app);
});
after(async () => {
server.server.close(() => {
console.log('Http server closed.');
});
auth.session.restore();
});
it('should allow access',async function () {
const response = await agent.get('/allowUser2');
expect(response.status).to.be.equal(200);
});
});
const chai = require('chai');
const chaiHttp = require('chai-http');
const { expect } = chai;
chai.use(chaiHttp);
let server;
let agent;
describe('route /allowUser2',() => {
before(async () => {
server = require('../../app.js')();
agent = chai.request.agent(server.app);
});
after(async () => {
server.server.close(() => {
console.log('Http server closed.');
});
});
it("shouldn't allow access",async function () {
const response = await agent.get('/allowUser2');
expect(response.status).to.be.equal(401);
});
});
工作repo
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。