gpt4 book ai didi

node.js - 作为 node express 中间件被跳过的 Sinon stub

转载 作者:搜寻专家 更新时间:2023-10-31 22:41:33 24 4
gpt4 key购买 nike

我正在尝试测试特定路线的行为。即使我创建 stub ,它也会继续运行中间件。我希望事件认证暂时通过。我知道此时它并不是真正的“单元”测试。我快到那里了。我还稍微简化了代码。这是要测试的代码:

const { rejectUnauthenticated } = require('../modules/event-authentication.middleware');

router.get('/event', rejectUnauthenticated, (req, res) => {
res.sendStatus(200);
});

这是我试图跳过的中间件:

const rejectUnauthenticated = async (req, res, next) => {
const { secretKey } = req.query;
if (secretKey) {
next();
} else {
res.status(403).send('Forbidden. Must include Secret Key for Event.');
}
};

module.exports = {
rejectUnauthenticated,
};

测试文件:

const chai = require('chai');
const chaiHttp = require('chai-http');
const sinon = require('sinon');
let app;
const authenticationMiddleware = require('../server/modules/event-authentication.middleware');

const { expect } = chai;
chai.use(chaiHttp);

describe('with correct secret key', () => {
it('should return bracket', (done) => {
sinon.stub(authenticationMiddleware, 'rejectUnauthenticated')
.callsFake(async (req, res, next) => next());

app = require('../server/server.js');

chai.request(app)
.get('/code-championship/registrant/event')
.end((err, response) => {
expect(response).to.have.status(200);
authenticationMiddleware.rejectUnauthenticated.restore();
done();
});
});
});

我试过以下其他类似问题:How to mock middleware in Express to skip authentication for unit test?还有这个:node express es6 sinon stubbing middleware not working但我仍然从应该跳过的中间件中得到 403。我还在 Debug模式下运行了测试,所以我知道应该 stub 的中间件函数仍在运行。

这是 stub 我的代码的问题吗?这是 ES6 问题吗?

我能否重组我的代码或测试以使其正常工作?

最佳答案

stub 代码确实存在问题。

当你需要你的服务器文件时

const app = require('../server/server.js');

您的应用是使用整套中间件创建的,包括 rejectUnauthenticated,并且对后者的引用存储在 app 中。

当你做的时候

sinon.stub(authenticationMiddleware, 'rejectUnauthenticated')
.callsFake(async (req, res, next) => next());

您替换了 authenticationMiddleware 模块的 rejectUnauthenticated 导出方法,但不是对已存储的原始 rejectUnauthenticated 的引用。

解决方案是创建应用程序(即 require('../server/server.js');)你模拟 exoprted 中间件方法之后:

const chai = require('chai');
const chaiHttp = require('chai-http');
const sinon = require('sinon');

// don't create app right away
let app;
const authenticationMiddleware = require('../server/modules/event-authentication.middleware');

const { expect } = chai;
chai.use(chaiHttp);

describe('with correct secret key', () => {
it('should return bracket', (done) => {
sinon.stub(authenticationMiddleware, 'rejectUnauthenticated')
.callsFake(async (req, res, next) => next());

// method is stubbed, you can create app now
app = require('../server/server.js');

chai.request(app)
.get('/code-championship/registrant/event')
.end((err, response) => {
expect(response).to.have.status(200);
authenticationMiddleware.rejectUnauthenticated.restore();
done();
});
});
});

关于node.js - 作为 node express 中间件被跳过的 Sinon stub ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53852873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com