无法使用 sinon 和 proxyquire 模拟构造函数
can't mock constructor using sinon and proxyquire
我看过几个类似的问题,但 none 个案例符合我的问题。我正在尝试模拟一个构造函数,我在其他测试中做过,但在使用 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)
});
});
单元测试解决方案如下:
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/Whosebug/58955304
我看过几个类似的问题,但 none 个案例符合我的问题。我正在尝试模拟一个构造函数,我在其他测试中做过,但在使用 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)
});
});
单元测试解决方案如下:
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/Whosebug/58955304