【问题标题】:Mocking ES6 BigQuery class模拟 ES6 BigQuery 类
【发布时间】:2020-06-20 23:29:39
【问题描述】:

我有一个包含基本大查询操作的小类:

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 方法编写 mocha 单元测试。然而,我一直被困在 js 中创建模拟。 Sinon、沙箱、存根等有很多选项。我认为我需要存根一个实例属性,例如,

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

但有些方法会一直尝试对谷歌进行实际身份验证,我也需要存根。任何关于如何开始的帮助都会很棒。

【问题讨论】:

    标签: node.js unit-testing google-bigquery mocha.js sinon


    【解决方案1】:

    你可以使用Link Seams with CommonJS,我们需要proxyquire来构造我们的接缝。

    例如

    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 |                   
    ----------|---------|----------|---------|---------|-------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-04
      • 2018-03-12
      • 2017-09-30
      • 1970-01-01
      • 2022-11-07
      • 1970-01-01
      • 2020-11-08
      • 2017-12-18
      相关资源
      最近更新 更多