Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sinon spy module export new class

I using mocha and sinon for unit test in node.js. I have a problem with mocking google auth library. This is part of code what want testing:

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

I'm trying testing "new GoogleAuth()" and OAuth2 but nothing work. This is my mock:

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

Error: expected stub to have been called with new

like image 706
Michał Niedziela Avatar asked Oct 23 '25 03:10

Michał Niedziela


1 Answers

Problem solved like this:

let OAuth2Mock = sinon.stub(); 
let googleMock = sinon.spy(function () 
{ return { OAuth2: OAuth2Mock } } );
like image 144
Michał Niedziela Avatar answered Oct 26 '25 00:10

Michał Niedziela



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!