Michał Niedziela
Michał Niedziela

Reputation: 309

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

Upvotes: 4

Views: 398

Answers (1)

Michał Niedziela
Michał Niedziela

Reputation: 309

Problem solved like this:

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

Upvotes: 3

Related Questions