sinon 间谍模块导出新 class

sinon spy module export new class

我在 node.js 中使用 mocha 和 sinon 进行单元测试。我在模拟 google auth 库时遇到问题。这是需要测试的部分代码:

const GoogleAuth = require('google-auth-library');
const auth = new GoogleAuth();
const oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

我正在尝试测试 "new GoogleAuth()" 和 OAuth2,但没有任何效果。 这是我的模拟:

let googleMock = sinon.stub().returns({
    Oauth2: sinon.spy()
});
....
it('should call new GoogleAuth', function ()
  {
       expect(googleMock).calledWithNew();
  });

错误:预期的存根已被调用为新的

问题是这样解决的:

let OAuth2Mock = sinon.stub(); 
let googleMock = sinon.spy(function () 
{ return { OAuth2: OAuth2Mock } } );