lundi 17 août 2015

Mocking a require()'d function using Sinon

I'm trying to mock request-promise in a test using Sinon. As far as I can tell, Sinon mocks methods of objects, and yet request-promise simply returns a function. Is there any way to mock a single required function?

var rp = require('request-promise');
var User = require('../../models/user');

// this works
sinon.stub(User, 'message', function() {});

// This is what I'd like to do to request-promise
sinon.stub(rp, function() {});

I've also looked into mockrequire and proxyquire but I think they both run into similar issues.

Aucun commentaire:

Enregistrer un commentaire