gpt4 book ai didi

javascript - 如何用 Sinon 模拟 pg Pool

转载 作者:行者123 更新时间:2023-11-29 23:05:15 25 4
gpt4 key购买 nike

在之前的项目中我模拟了 mysql librarySinon .我是这样做的:

X.js:

const con = mysql.createPool(config.mysql);
...

项目中的其他地方:

const rows = await con.query(query, inserts);
...

X.test.js:

const sinon = require('sinon');
const mockMysql = sinon.mock(require('mysql'));
...

mockMysql.expects('createPool').returns({
query: () => {
// Handles the query...
},
...

它工作得很好。

在另一个项目中,我试图模拟 pg ,又是诗乃。

pool.js:

const { Pool } = require('pg');
const config = require('@blabla/config');
const pool = new Pool(config.get('database'));

module.exports = pool;

项目中的其他地方:

const con = await pool.connect();
const result = await con.query(...

Y.test.js:

???

我无法理解如何模拟 connect().query()。以下方法均无效:

1:

const { Pool } = require('pg');
const config = require('@blabla/config');

const mockPool = sinon.mock(new Pool(config.get('database')));
...
mockPool.expects('connect').returns({
query: () => {
console.log('query here');
},
});

1 没有错误,但使用了真正的数据库连接。

2:

const { Pool } = sinon.mock(require('pg'));
const config = require('@blabla/config');

const pool = new Pool(config.get('database'));

pool.expects('connect').returns({
query: () => {
console.log('query here');
},
});

2 => TypeError: Pool 不是构造函数

3:

const { Pool } = sinon.mock(require('pg'));
const config = require('@blabla/config');

const pool = sinon.createStubInstance(Pool);
pool.connect.returns({
query: () => {
console.log('query here');
},
});

3 => TypeError: 构造函数应该是一个函数。

谁能给我指明正确的方向,告诉我如何模拟我的 PostgreSQL 连接?

最佳答案

示例:我有这样的 postgres.js。

const { Pool } = require('pg');

const handler = {
count: async (pgQuery) => {
try {
const pool = new Pool();
const res = await pool.query(pgQuery);
return { count: parseInt(res.rows[0].counter, 10) };
} catch (error) {
// Log/Throw error here.
}
return false;
}
}

module.exports = handler;

我在 postgres.spec.js 上创建的 spec 测试是这样的。

const { expect } = require('chai');
const sinon = require('sinon');
const pgPool = require('pg-pool');
const handler = require('postgres.js');

describe('Postgres', function () {
it('should have method count that bla bla', async function () {
// Create stub pgPool query.
const postgreeStubQuery = sinon.stub(pgPool.prototype, 'query');
postgreeStubQuery.onFirstCall().throws('XXX');
postgreeStubQuery.onSecondCall().resolves({
rows: [{ counter: 11 }],
});

// Catch case.
const catcher = await handler.count('SELECT COUNT()..');
expect(catcher).to.equal(false);
expect(postgreeStubQuery.calledOnce).to.equal(true);

// Correct case.
const correct = await handler.count('SELECT COUNT()..');
expect(correct).to.deep.equal({ count: 11 });
expect(postgreeStubQuery.calledTwice).to.equal(true);

// Restore stub.
postgreeStubQuery.restore();
});
});

要 stub pool.query(),您需要 stub pg-pool原型(prototype)和方法查询。

希望这对您有所帮助。

关于javascript - 如何用 Sinon 模拟 pg Pool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54887149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com