How to mock pg Pool with Sinon(如何用SINON模拟PG Pool)
问题描述
在上一个项目中,我用Sinon模拟了mysql library。我是这样做的:
X.js
:
const con = mysql.createPool(config.mysql);
...
Some other place in the project
:
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,同样是用sinon。
pool.js
:
const { Pool } = require('pg');
const config = require('@blabla/config');
const pool = new Pool(config.get('database'));
module.exports = pool;
Some other place in the project
:
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 is not a constructor
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: The constructor should be a function.
有人能就如何模拟我的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上创建的规范测试如下所示。
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();
});
});
若要存根pool.Query(),您需要存根pg-pool原型和方法查询。
希望这能有所帮助。
这篇关于如何用SINON模拟PG Pool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何用SINON模拟PG Pool


- Flexslider 箭头未正确显示 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01