I had a block of post request for which I need to stub response inside non-exported function as mentioned below:
function callMicroserive(a, b, c){
//before post request code part
request.post(requestoptions, function (error, httpResponse, body) {//code}
//after post request code part
}
I don't want to stub and return callMicroserive function on a whole but need to stub http post request inside function.
I tried to implement using sinon along with rewire setter and getter methods.But I can't make to stub http request block alone. Can anyone provide code template for this issue?
Proxyquire
Tried with proxyquire as mentioned in below:
const postcall = proxyquire('../plugins/routes/handlers/contentservicehandler.js', {post : {httpResponse:{statusCode : '200'},'@noCallThru': true}});
postcall();
Sinon With Sinon, tried below ways:
var request = require('request');
sinon.stub(request, 'post').returns(//jsonObject);
also tried
sinon.yields(null, httpresponse,body)// to send arguments to callback
Rewire
const contentHandler = rewire('../plugins/routes/handlers/contentservicehandler.js');
const postCall = {post:contentHandler.__get__('request.post')};
var stub = sinon.stub(postCall,'post').returns(//json);
contentHandler.__set__('post', stub);
Getting error 'Invalid URI /' when call to request.post() is called with all unit packages. Looks like it is hitting server for actual response.