Reputation:
I have something like:
sandbox.stub(rp, 'get').resolves(successResponse)
which returns my custom response when it hits this code:
return await rp.get(url, options)
But how can I do something like this:
sandbox.stub(rp).resolves(successResponse)
Which can return a custom response when it hits this code?
return await rp(url, options)
When I attempt "stubbing" the entire object, I get this error when I run the test:
TypeError: Attempted to wrap undefined property undefined as function
at wrapMethod (node_modules\sinon\lib\sinon\util\core\wrap-method.js:70:21)
at stub (node_modules\sinon\lib\sinon\stub.js:58:44)
at Object.stub (node_modules\sinon\lib\sinon\collection.js:93:33)
rp
is request-promise-native, which wraps request
Upvotes: 0
Views: 1178
Reputation: 1622
You might find the Sinon HowTo about Link Seams helpful: http://sinonjs.org/how-to/link-seams-commonjs/
Upvotes: 0
Reputation:
From @Troopers' link in the comments above, it seems like this isn't possible: Doing this is not technically possible without faking the entire module loading system.
So I followed the suggestion here: https://github.com/request/request/issues/2181 and used mock-require to stub rp
. I also changed up my code that used to call rp.get()
, so that it just calls rp()
, since I couldn't also figure out how to stub both rp()
and rp.get()
Upvotes: 1