如何用 sinon 存根 oracledb? [英] How to stub oracledb with sinon?
本文介绍了如何用 sinon 存根 oracledb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的函数,一旦它从 oracle 数据库中获取数据,它将返回一个承诺:
Here is my function which will return a promise once it gets data from oracle database:
const getDataFromOracleDB = (filter, query) =>
new Promise(async (resolve, reject) => {
let conn;
try {
conn = await oracledb.getConnection(dbConfig);
const result = await conn.execute(query, [filter]);
const { rows } = result;
...
catch (err) {
...
}
};
作为单元测试,我想存根 conn.execute
,但不知道如何做.我已经试过了:
As the unit test, I want to stub conn.execute
, but have no idea how to do that. I've treid:
const stub = sinon.stub(conn, 'execute').returns([1, 2, 3]);
但是得到了:
TypeError: Cannot stub non-existent own property execute
有什么建议吗?
推荐答案
我无法用您提供的代码复制错误,但也许这个快速模型会有所帮助:
I can't replicate the error with the code you supplied, but perhaps this quick mockup will help:
const chai = require('chai');
const sinon = require('sinon');
const oracledb = require('oracledb');
const config = require('./dbConfig.js');
const expect = chai.expect;
sinon.stub(oracledb, 'getConnection').resolves({
execute: function() {},
close: function() {}
});
describe('Parent', () => {
describe('child', () => {
it('should work', async (done) => {
let conn;
try {
conn = await oracledb.getConnection(config);
sinon.stub(conn, 'execute').resolves({
rows: [[2]]
});
let result = await conn.execute(
'select 1 from dual'
);
expect(result.rows[0][0]).to.equal(2);
done();
} catch (err) {
done(err);
} finally {
if (conn) {
try {
await conn.close();
} catch (err) {
console.error(err);
}
}
}
});
});
});
查询通常会返回值 1,但这会返回 2 并通过.
The query would normally return a value of 1, but this returns 2 and passes.
这篇关于如何用 sinon 存根 oracledb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文