Sinon 如何在 mysql2.createPool 函数中对双箭头函数进行单元测试
Sinon how to unit test double arrow function within mysql2.createPool function
我有一个连接 class 用于连接到 AWS RDS Aurora 数据库实例。 class 工作正常,但我无法获得完整的单元测试覆盖率。有一件我不确定如何涵盖。它是下面连接 class 中显示的 mysql_clear_password: () => () => Buffer.from(this.options.password + '[=13=]')
。我怎样才能覆盖那条特定的线?是否需要重构函数?
我已经尝试将 Buffer
函数移动到一个单独的函数,但覆盖率报告仍然显示原始行未被覆盖
连接class:
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 + '[=11=]')
}
});
}
}
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);
});
});
使用stub.callsFake
方法使存根(mysql2.createPool
) 在调用时调用提供的函数。然后,您可以从测试用例中提供的函数中获取 mysql_clear_password
方法。
例如
connection.js
:
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 + '[=10=]'),
},
});
}
}
module.exports = { Connection };
connection.test.js
:
const mysql2 = require('mysql2/promise');
const conns = require('./connection');
const sinon = require('sinon');
const { expect } = require('chai');
describe('64300458', () => {
it('Test creatPool function from connection class', () => {
const options = {
host: 'testHost',
user: 'testUser',
password: 'testPassword',
};
let configRef;
const createPoolStub = sinon.stub(mysql2, 'createPool').callsFake((config) => {
configRef = config;
});
const conn = new conns.Connection(options);
conn.createPool();
sinon.assert.calledOnce(createPoolStub);
// test mysql_clear_password
const actual = configRef.authPlugins.mysql_clear_password()();
expect(actual).to.be.eql(Buffer.from('testPassword[=11=]'));
createPoolStub.restore();
});
});
带有覆盖率报告的单元测试结果:
64300458
✓ Test creatPool function from connection class
1 passing (11ms)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 0 | 100 | 100 |
connection.js | 100 | 0 | 100 | 100 | 4
---------------|---------|----------|---------|---------|-------------------
我有一个连接 class 用于连接到 AWS RDS Aurora 数据库实例。 class 工作正常,但我无法获得完整的单元测试覆盖率。有一件我不确定如何涵盖。它是下面连接 class 中显示的 mysql_clear_password: () => () => Buffer.from(this.options.password + '[=13=]')
。我怎样才能覆盖那条特定的线?是否需要重构函数?
我已经尝试将 Buffer
函数移动到一个单独的函数,但覆盖率报告仍然显示原始行未被覆盖
连接class:
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 + '[=11=]')
}
});
}
}
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);
});
});
使用stub.callsFake
方法使存根(mysql2.createPool
) 在调用时调用提供的函数。然后,您可以从测试用例中提供的函数中获取 mysql_clear_password
方法。
例如
connection.js
:
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 + '[=10=]'),
},
});
}
}
module.exports = { Connection };
connection.test.js
:
const mysql2 = require('mysql2/promise');
const conns = require('./connection');
const sinon = require('sinon');
const { expect } = require('chai');
describe('64300458', () => {
it('Test creatPool function from connection class', () => {
const options = {
host: 'testHost',
user: 'testUser',
password: 'testPassword',
};
let configRef;
const createPoolStub = sinon.stub(mysql2, 'createPool').callsFake((config) => {
configRef = config;
});
const conn = new conns.Connection(options);
conn.createPool();
sinon.assert.calledOnce(createPoolStub);
// test mysql_clear_password
const actual = configRef.authPlugins.mysql_clear_password()();
expect(actual).to.be.eql(Buffer.from('testPassword[=11=]'));
createPoolStub.restore();
});
});
带有覆盖率报告的单元测试结果:
64300458
✓ Test creatPool function from connection class
1 passing (11ms)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 0 | 100 | 100 |
connection.js | 100 | 0 | 100 | 100 | 4
---------------|---------|----------|---------|---------|-------------------