Как заглушить oracledb с помощью sinon? - PullRequest
0 голосов
/ 04 мая 2018

Вот моя функция, которая будет возвращать обещание, как только получит данные из базы данных Oracle:

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, но понятия не имею, как это сделать. Я трейд:

const stub = sinon.stub(conn, 'execute').returns([1, 2, 3]);

Но получил:

TypeError: Cannot stub non-existent own property execute

Есть предложения?

1 Ответ

0 голосов
/ 04 мая 2018

Я не могу воспроизвести ошибку с предоставленным вами кодом, но, возможно, этот быстрый макет поможет:

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 и проходит.

...