2

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

代码.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;
}

测试.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)
    });
});
4

1 回答 1

6

这是单元测试解决方案:

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;
  beforeEach(() => {
    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

于 2019-11-21T06:50:58.403 回答