Sinon单元测试MySQL连接

问题描述 投票:0回答:1

我正在尝试对我的AWS Node Lambda进行单元测试。我正在使用MySQL。我有一个实用程序文件来获取MySQL连接池,这是我的处理程序中的依赖项。我正在尝试通过Mocha和Sinon对我的处理程序进行单元测试。我想存根或模拟数据库池和连接(实际上没有创建数据库连接或命中数据库),但是我没有任何运气。有谁知道如何实现这一目标?我创建了以下2个文件作为测试工具:

dbConn.js

const mysql = require('mysql2/promise');

async function getPool(options = {}) {
  return await mysql.createPool(optionsClone);
}

module.exports = {
  getPool
};

getEmployees.js

const database = require('./dbConn');

exports.handler = async function(event, context, callback) {
  // Connect to a database via connection pool
  let pool = await database.getPool(dbOptions);
  let conn = await pool.getConnection();

  const dbResult = await conn.query('select * from employees');

  conn.release();

  return dbResult;
};
javascript mysql unit-testing aws-lambda sinon
1个回答
0
投票

这里是单元测试解决方案:

dbConn.js

const mysql = require("mysql2/promise");

async function getPool(options = {}) {
  return await mysql.createPool(optionsClone);
}

module.exports = {
  getPool,
};

getEmployees.js

const database = require("./dbConn");

exports.handler = async function(event, context, callback) {
  const dbOptions = {};
  let pool = await database.getPool(dbOptions);
  let conn = await pool.getConnection();

  const dbResult = await conn.query("select * from employees");

  conn.release();

  return dbResult;
};

getEmployees.test.js

const { handler } = require("./getEmployees.js");
const database = require("./dbConn");
const sinon = require("sinon");
const { expect } = require("chai");

describe("getEmployees", () => {
  afterEach(() => {
    sinon.restore();
  });
  it("should pass", async () => {
    const connStub = { query: sinon.stub().resolves({ rowCount: 1 }), release: sinon.stub() };
    const poolStub = { getConnection: sinon.stub().resolves(connStub) };
    sinon.stub(database, "getPool").resolves(poolStub);
    const actual = await handler();
    expect(actual).to.be.eql({ rowCount: 1 });
    sinon.assert.calledWith(database.getPool, {});
    sinon.assert.calledOnce(poolStub.getConnection);
    sinon.assert.calledWith(connStub.query, "select * from employees");
    sinon.assert.calledOnce(connStub.release);
  });
});

带有覆盖率报告的单元测试结果:

  getEmployees
    ✓ should pass


  1 passing (13ms)

----------------------|----------|----------|----------|----------|-------------------|
File                  |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------------|----------|----------|----------|----------|-------------------|
All files             |    96.43 |        0 |       80 |    96.43 |                   |
 dbConn.js            |    66.67 |        0 |        0 |    66.67 |                 4 |
 getEmployees.js      |      100 |      100 |      100 |      100 |                   |
 getEmployees.test.js |      100 |      100 |      100 |      100 |                   |
----------------------|----------|----------|----------|----------|-------------------|

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59346368

© www.soinside.com 2019 - 2024. All rights reserved.