Nepoxx Nepoxx - 10 months ago 109
Node.js Question

Stubbing a promisified function with sinon and bluebird

In the file I would like to test, I have the following code:

var httpGet = Promise.promisify(require("request").get);
httpGet(endpoint, {
auth: {bearer: req.body.access_token},
json: true

Now, in my tests, I want to make sure that httpGet was called once, and make sure the parameters are valid. Before being promisified, my test looked like this:

beforeEach(function () {
request.get = sinon.stub()
.yields(null, null, {error: "test error", error_description: "fake google error."});

afterEach(function () {
var requestArgs = request.get.args[0];
var uri = requestArgs[0];


Unfortunately this no longer works when request.get is promisified. I tried stubbing request.getAsync instead (since bluebird appends "Async" to promisified functions), but that does not work either. Any ideas?


Promise.promisify doesn't modify the object, it simply takes a function and returns a new function, it is completely unaware that the function even belongs to "request".

"Async" suffixed methods are added to the object when using promisify All


request.getAsync = sinon.stub()
        .yields(null, null, {error: "test error", error_description: "fake google error."});