【问题标题】:Sinon how to unit test double arrow function within mysql2.createPool functionSinon如何在mysql2.createPool函数中对双箭头函数进行单元测试
【发布时间】: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


    【解决方案1】:

    使用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 + '\0'),
          },
        });
      }
    }
    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\0'));
        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                 
    ---------------|---------|----------|---------|---------|-------------------
    

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 1970-01-01
      • 2019-04-21
      • 2021-07-16
      • 1970-01-01
      • 2021-05-21
      • 2014-02-08
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多