模拟ES6 BigQuery类 [英] Mocking ES6 BigQuery class

查看:44
本文介绍了模拟ES6 BigQuery类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小类,其中包装了一个基本的大查询操作:

I have a small class which wraps a basic big query operation:

const { BigQuery } = require('@google-cloud/bigquery');


export default class BQ {
    bigquery;

    constructor(projectId, keyFilename) {
        const options = {
            projectId,
            keyFilename,
            autoRetry: true,
        };
        
        this.bigquery = new BigQuery(options);
    }

    async query(query) {
        const options = {
            query,
            location: 'us',
        };
        const [job] = await this.bigquery.createQueryJob(options);
        const [rows] = await job.getQueryResults();
        return rows;
    }
}

我正在尝试为 query 方法编写一个摩卡单元测试.但是,我一直坚持在js中创建模拟程序.Sinon,沙箱,存根等都有很多选项.我认为我需要对实例属性进行存根,

I am trying to write a mocha unit test for the query method. However, I keep getting stuck with creating mocks in js. There are many options with Sinon, sandboxes, stubs, etc. I would think that I need to stub an instance attribute like,

const bq = new BQ(projectId, keyFilename);
const bqStub = sandbox.stub(bq, 'bigquery');

但是有些方法也一直尝试对我进行实际身份验证,但我也需要将其存根.任何有关入门的帮助都将很棒.

But there are methods that keep trying to actually auth to google that I need stubbed too. Any help on how to get started would be wonderful.

推荐答案

您可以使用使用CommonJS链接接缝,我们需要 proxyquire 来构造接缝.

You can use Link Seams with CommonJS, we need proxyquire to construct our seams.

例如

bq.js :

const { BigQuery } = require('@google-cloud/bigquery');

export default class BQ {
  bigquery;

  constructor(projectId, keyFilename) {
    const options = {
      projectId,
      keyFilename,
      autoRetry: true,
    };

    this.bigquery = new BigQuery(options);
  }

  async query(query) {
    const options = {
      query,
      location: 'us',
    };
    const [job] = await this.bigquery.createQueryJob(options);
    const [rows] = await job.getQueryResults();
    return rows;
  }
}

bq.test.js :

import proxyquire from 'proxyquire';
import sinon from 'sinon';

describe('62492844', () => {
  it('should pass', async () => {
    const rows = [{ id: 1 }, { id: 2 }];
    const job = {
      getQueryResults: sinon.stub().returns([rows]),
    };
    const bigquery = {
      createQueryJob: sinon.stub().returns([job]),
    };
    const BigQueryStub = sinon.stub().returns(bigquery);
    const BQ = proxyquire('./bq', {
      '@google-cloud/bigquery': { BigQuery: BigQueryStub },
    }).default;

    const bq = new BQ('projectId', './svc.json');
    sinon.assert.calledWithExactly(BigQueryStub, {
      projectId: 'projectId',
      keyFilename: './svc.json',
      autoRetry: true,
    });
    const actual = await bq.query('query');
    sinon.assert.calledWithExactly(bigquery.createQueryJob, { query: 'query', location: 'us' });
    sinon.assert.calledOnce(job.getQueryResults);
    sinon.assert.match(actual, rows);
  });
});

单元测试结果:

  62492844
    ✓ should pass (2427ms)


  1 passing (2s)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 bq.ts    |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------

这篇关于模拟ES6 BigQuery类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆