Sinon存根被跳过作为节点表达中间件 [英] Sinon stub being skipped as node express middleware

查看:90
本文介绍了Sinon存根被跳过作为节点表达中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试特定路线的行为.即使我创建存根,它也会继续运行中间件.我希望事件认证暂时通过.我了解到,这并不是真正的单元"测试.我快到那里了.我还简化了代码.这是要测试的代码:

I'm trying to test the behavior of a particular route. It continues to run the middleware even when I create a stub. I want the event authentication to simply pass for now. I understand that it's not truly a "unit" test at this point. I'm getting there. I've also simplified the code a little. Here is the code to test:

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

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

这是我要跳过的中间件:

Here is the middleware I am trying to skip:

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();
      });
  });
});

我已经尝试过以下类似的其他问题:节点表示es6 sinon stubbing中间件不起作用,但是我仍然从中间件中获取403,应该跳过该部分.我还以调试模式运行了测试,因此我知道应该存根的中间件功能仍在运行.

I've tried following other similar questions like this: How to mock middleware in Express to skip authentication for unit test? and this: node express es6 sinon stubbing middleware not working but I'm still getting the 403 from the middleware that should be skipped. I also ran the tests in debug mode, so I know the middleware function that should be stubbed is still running.

存根我的代码是否有问题?这是ES6问题吗?

Is this an issue with stubbing my code? Is this an ES6 issue?

我可以重组代码或测试以使其正常工作吗?

Can I restructure my code or the test to make this work?

推荐答案

存根代码确实存在问题.

There is an issue indeed with stubbing your code.

当您需要服务器文件时

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

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

your app is get created with the whole set of middlewares, including rejectUnauthenticated, and a reference to the latter is stored inside app.

这样做的时候

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

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

you replace the rejectUnauthenticated exported method of authenticationMiddleware module, but not the reference to original rejectUnauthenticated that is already stored.

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

The solution is to create the app (i.e. require('../server/server.js');) after you mock the exoprted middleware method:

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();
      });
  });
});

这篇关于Sinon存根被跳过作为节点表达中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆