简体   繁体   English

Sinon 单元测试 MySQL 连接

[英]Sinon unit testing MySQL connection

I am trying to unit test my AWS Node Lambda.我正在尝试对我的 AWS 节点 Lambda 进行单元测试。 I am using MySQL.我正在使用 MySQL。 I have a utility file to get a MySQL connection pool, which is a dependency in my handler.我有一个实用程序文件来获取 MySQL 连接池,这是我的处理程序中的一个依赖项。 I am trying to unit test my handler via Mocha and Sinon.我正在尝试通过 Mocha 和 Sinon 对我的处理程序进行单元测试。 I want to stub or mock the database pool and connection (without actually creating a db connection or hitting the database), but I am not having any luck.我想存根或模拟数据库池和连接(没有实际创建数据库连接或访问数据库),但我没有任何运气。 Does anyone know how to achieve this?有谁知道如何实现这一目标? I created the following 2 files as a test harness:我创建了以下 2 个文件作为测试工具:

dbConn.js数据库连接器

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

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

module.exports = {
  getPool
};

getEmployees.js 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;
};

Here is the unit test solution:这是单元测试解决方案:

dbConn.js : dbConn.js :

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

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

module.exports = {
  getPool,
};

getEmployees.js : 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 : 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);
  });
});

Unit test result with coverage report:带有覆盖率报告的单元测试结果:

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

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

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

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