简体   繁体   English

来自中间件的 Sinon 存根模块的 function

[英]Sinon stub out module's function from a middleware

Based on this question , I need to also make a test for a middleware which also uses the db-connection.js file.基于这个问题,我还需要对也使用db-connection.js文件的中间件进行测试。 The middleware file will look like this:中间件文件将如下所示:

const dbConnection = require('./db-connection.js')

module.exports = function (...args) {
   return async function (req, res, next) {
      // somethin' somethin' ...
      const dbClient = dbConnection.db
      const docs = await dbClient.collection('test').find()
 
      if (!docs) {
         return next(Boom.forbidden())
      }
   }
}

, the database connection file do not change, which is: ,数据库连接文件不变,即:

const MongoClient = require('mongodb').MongoClient
const dbName = 'test'
const url = process.env.MONGO_URL

const client = new MongoClient(url, { useNewUrlParser: true,
  useUnifiedTopology: true,
  bufferMaxEntries: 0 // dont buffer querys when not connected
})

const init = () => {
  return client.connect().then(() => {
    logger.info(`mongdb db:${dbName} connected`)

    const db = client.db(dbName)
  })
}

/**
 * @type {Connection}
 */
module.exports = {
  init,
  client,
  get db () {
    return client.db(dbName)
  }
}

How the middleware works is by passing list of strings (that strings is roles), I have to query to the database and check whether there is a record of each roles.中间件的工作原理是通过传递字符串列表(字符串是角色),我必须查询数据库并检查是否有每个角色的记录。 If the record exists, I will return next() , while if the record does not exist, I will return next(Boom.forbidden()) (next function with a 403 status code from Boom module).如果记录存在,我将返回next() ,而如果记录不存在,我将返回next(Boom.forbidden()) (下一个 function 带有来自 Boom 模块的 403 状态码)。

Given the details above, how does one make a test to test out the return value of the middleware if the record exists or not?鉴于以上细节,如果记录存在与否,如何进行测试以测试中间件的返回值? This means I have to assert the next() and next(Boom.forbidden) to be exact.这意味着我必须准确地断言next()next(Boom.forbidden)

Based on the answer .根据答案 You can create stubs for the req , res objects, and next function.您可以为reqres对象和next function 创建存根。

Eg( Doesn't run, but it should work. )例如(不运行,但它应该工作。

const sinon = require('sinon');

describe('a', () => {
  afterEach(() => {
    sinon.restore();
  });
  it('should find some docs', async () => {
    process.env.MONGO_URL = 'mongodb://localhost:27017';
    const a = require('./a');
    const dbConnection = require('./db-connection.js');

    const dbStub = {
      collection: sinon.stub().returnsThis(),
      find: sinon.stub(),
    };
    sinon.stub(dbConnection, 'db').get(() => dbStub);
    const req = {};
    const res = {};
    const next = sinon.stub();
    const actual = await a()(req, res, next);
    sinon.assert.match(actual, true);
    sinon.assert.calledWithExactly(dbStub.collection, 'test');
    sinon.assert.calledOnce(dbStub.find);
    sinon.assert.calledOnce(next);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM