不能使用sinon和proxyquire模拟构造函数

我看过几个类似的问题,但没有一个案例适合我的问题。我正在尝试模拟一个在其他测试中已经完成的构造函数,但是在使用google-auth-library的情况下我无法使其正常工作

code.js

const {OAuth2Client} = require('google-auth-library');
const keys = require('./oauth2.keys.json');

async function getRedirectUrl() {
  const oAuth2Client = new OAuth2Client(
    keys.installed.client_id,keys.installed.client_secret,keys.installed.redirect_uris[0]
  );

  const authorizeUrl = oAuth2Client.generateAuthUrl({
    access_type: 'offline',scope: 'https://www.googleapis.com/auth/bigquery',prompt: 'consent'
  });

  return authorizeUrl;
}

test.js

let Code = require('../code.js');

describe('code',function() {
    let generateUrlStub,tokenStub,mockClient;

    before(async () => {
      generateUrlStub = sinon.stub().returns('http://example.com');
      tokenStub = sinon.stub().returns({tokens: 'tokens'});

      mockClient = sinon.stub().returns({
        generateAuthUrl: generateUrlStub,getToken: tokenStub,});

      Code = proxyquire('../Code.js',{
        'google-auth-library': mockClient,});
    });

    it('should call generateAuthUrl',async function() {
      const output = await Code.getRedirectUrl();
      sinon.assert.called(generateUrlStub)
    });
});
abxtoo 回答:不能使用sinon和proxyquire模拟构造函数

这是单元测试解决方案:

const { OAuth2Client } = require("google-auth-library");
const keys = {
  installed: {
    client_id: "1",client_secret: "client_secret",redirect_uris: ["http://example.com/callback"]
  }
};

async function getRedirectUrl() {
  const oAuth2Client = new OAuth2Client(
    keys.installed.client_id,keys.installed.client_secret,keys.installed.redirect_uris[0]
  );

  const authorizeUrl = oAuth2Client.generateAuthUrl({
    access_type: "offline",scope: "https://www.googleapis.com/auth/bigquery",prompt: "consent"
  });

  return authorizeUrl;
}

module.exports = { getRedirectUrl };

index.spec.js

const proxyquire = require("proxyquire");
const sinon = require("sinon");
const { expect } = require("chai");

describe("code",function() {
  let generateUrlStub,tokenStub,code;
  before(() => {
    generateUrlStub = sinon.stub().returns("http://example.com");
    tokenStub = sinon.stub().returns({ tokens: "tokens" });

    code = proxyquire("./",{
      "google-auth-library": {
        OAuth2Client: sinon.stub().callsFake(() => {
          return {
            generateAuthUrl: generateUrlStub,getToken: tokenStub
          };
        })
      }
    });
  });
  afterEach(() => {
    sinon.restore();
  });

  it("should call generateAuthUrl",async function() {
    const output = await code.getRedirectUrl();
    expect(output).to.be.eq("http://example.com");
    sinon.assert.called(generateUrlStub);
  });
});

覆盖率100%的单元测试结果:

  code
    ✓ should call generateAuthUrl


  1 passing (216ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |      100 |      100 |                   |
 index.js      |      100 |      100 |      100 |      100 |                   |
 index.spec.js |      100 |      100 |      100 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/58955304

本文链接:https://www.f2er.com/3065580.html

大家都在问