【发布时间】:2020-10-11 03:57:07
【问题描述】:
我有一个 Connection 类,用于连接到 AWS RDS Aurora 数据库实例。该类工作正常,但我无法获得完整的单元测试覆盖率。有一件我不知道如何覆盖。它是mysql_clear_password: () => () => Buffer.from(this.options.password + '\0'),显示在下面的 Connection 类中。我怎样才能覆盖那条特定的线?是否需要重构函数?
我已尝试将 Buffer 函数移至单独的函数,但覆盖率报告仍显示原始行未被覆盖
连接类:
const mysql2 = require('mysql2/promise');
class Connection {
constructor(options = {}) {
this.options = options;
}
createPool () {
this.pool = mysql2.createPool({
host: this.options.host,
user: this.options.user,
database: 'my_db',
ssl: 'Amazon RDS',
password: this.options.password,
authPlugins: {
mysql_clear_password: () => () => Buffer.from(this.options.password + '\0')
}
});
}
}
module.exports = { Connection };
这是我目前的测试结果:
const conns = require('../src/connection');
const sinon = require('sinon');
const mysql2 = require('mysql2/promise');
describe('connection', () => {
afterEach(() => {
sinon.restore();
});
test('Test creatPool function from connection class', async () => {
const options = {
host: 'testHost',
user: 'testUser',
password: 'testPassword'
};
const createPoolStub = sinon.stub(mysql2, 'createPool').returns(sinon.stub().returnsThis());
const conn = new conns.Connection(options);
await conn.createPool();
sinon.assert.calledOnce(createPoolStub);
});
});
【问题讨论】:
标签: node.js unit-testing aws-lambda amazon-rds sinon