SINON存根输出模块的功能

假设我这里有一个文件:

// a.js
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())
      }
   }
}

db-connection.js如下所示:

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

并且我有一个测试来存根find()方法以至少返回TRUE(以便测试a.js的返回值是否为TRUE。我该如何做?


解决方案

单元测试策略是存根DB连接,我们不需要连接真正的Mongo DB服务器。以便我们可以在与外部环境隔离的环境中运行测试用例。

您可以使用stub.get(getterFn)替换dbConnection.db的新getter。

因为MongoDB客户端初始化发生在您需要db-connection模块时。在需要adb-connection模块之前,您应该从环境变量中为MongoClient提供正确的URL。否则,MongoDB NodeJS驱动程序将抛出无效的url错误。

例如

a.js

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

module.exports = function () {
  const dbClient = dbConnection.db;
  const docs = dbClient.collection('test').find();

  if (!docs) {
    return true;
  }
};

db-connection.js

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

const client = new MongoClient(url, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

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

module.exports = {
  init,
  client,
  get db() {
    return client.db(dbName);
  },
};

a.test.js

const sinon = require('sinon');

describe('a', () => {
  afterEach(() => {
    sinon.restore();
  });
  it('should find some docs', () => {
    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 actual = a();
    sinon.assert.match(actual, true);
    sinon.assert.calledWithExactly(dbStub.collection, 'test');
    sinon.assert.calledOnce(dbStub.find);
  });
});

测试结果:

  a
    ✓ should find some docs (776ms)


  1 passing (779ms)

------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------|---------|----------|---------|---------|-------------------
All files         |      75 |       50 |      25 |      75 |                   
 a.js             |     100 |       50 |     100 |     100 | 7                 
 db-connection.js |      60 |      100 |       0 |      60 | 11-13,21          
------------------|---------|----------|---------|---------|-------------------

相关文章