mercredi 20 novembre 2019

can't mock constructor using sinon and proxyquire

I've looked at several similar questions but none of the cases fit my problem. I'm trying to mock a constructor, which I've done in other tests, but I can't get it to work in the case of using 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)
    });
});

Aucun commentaire:

Enregistrer un commentaire