您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Sinon存根被跳过作为节点表达中间件

Sinon存根被跳过作为节点表达中间件

存根代码确实存在问题。

当您需要服务器文件

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

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

当你做

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

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

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

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();
      });
  });
});
其他 2022/1/1 18:14:35 有577人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶