Reputation: 10691
I am trying to unit test this function with sinon/mocha which uses request-promise
. I attach the .promise()
to allow access to all the Bluebird promise methods.
According to the request-promise
readme:
rp(...).promise() or e.g. rp.head(...).promise() which returns the underlying promise so you can access the full Bluebird API
myModule.js var requestPromise = require('request-promise');
function requestWrapper(opts) {
// Adds .promise() to allow access to Bluebird methods
return requestPromise(opts).promise();
}
module.exports = requestWrapper;
Unit test
var Promise = require('bluebird');
var requestPromise = sinon.stub().returns(Promise.resolve());
var rewire = require('rewire');
var myModule = rewire('./myModule');
myModule.__set__({
requestPromise: requestPromise
});
describe('myModule', function() {
var testPromise;
it('should...', function() {
testPromise = myModule.requestWrapper({ method: 'GET', url: 'http://someurl.com' })
});
});
Error
I'm getting the following error: TypeError: requestPromise(...).promise is not a function
.
Upvotes: 1
Views: 1720
Reputation: 950
You're mocking is wrong (but close).
request-promise
is a function that returns an object that has a function promise
that returns the underlying promise.
What you've done with your stub & rewire is set request-promise
to be a function that returns a promise.
A possible fix:
var requestPromise = sinon.stub().returns({ promise: () => Promise.resolve() });
Upvotes: 1