簡體   English   中英

Sinon 單元測試 MySQL 連接

[英]Sinon unit testing MySQL connection

我正在嘗試對我的 AWS 節點 Lambda 進行單元測試。 我正在使用 MySQL。 我有一個實用程序文件來獲取 MySQL 連接池,這是我的處理程序中的一個依賴項。 我正在嘗試通過 Mocha 和 Sinon 對我的處理程序進行單元測試。 我想存根或模擬數據庫池和連接(沒有實際創建數據庫連接或訪問數據庫),但我沒有任何運氣。 有誰知道如何實現這一目標? 我創建了以下 2 個文件作為測試工具:

數據庫連接器

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

這是單元測試解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM