0

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.

Nazeer_hanne
  • 213
  • 6
  • 20
  • Can provide a little more context? Is this a file you are testing from a different file? – Enslev Jan 16 '19 at 12:05
  • yes. I had one unittest js file from which I need to test service APIs present in other js file – Nazeer_hanne Jan 16 '19 at 12:11
  • Have a look at my answer [here](https://stackoverflow.com/a/48115900/4423258), and see if it helps. – Enslev Jan 16 '19 at 14:45
  • @Enslev, No. It didn't supported in my case – Nazeer_hanne Jan 17 '19 at 09:17
  • Are you sure? Sounds to me like you want hijack the request module, to stub `request.post()`. That is exactly what `proxyquire` does. – Enslev Jan 17 '19 at 13:08
  • const postcall = proxyquire('../plugins/routes/handlers/contentservicehandler.js', {post : {httpResponse:{statusCode : '200'},'@noCallThru': true}}); postcall(); @Enslev, I tried like this to stub post method and avoid callthru server. But not worked. Please point out where it went wrong – Nazeer_hanne Jan 30 '19 at 08:53
  • @enslev, Getting error 'Invalid URI /' when call to request.post() is called. Looks like it is hitting server for actual response. – Nazeer_hanne Jan 30 '19 at 08:58
  • @Nazeer_hanne can you show how you were trying to stub `request`? – James Jan 30 '19 at 09:45
  • @James, please go through my question to know stubbing of request with different unit packages – Nazeer_hanne Jan 30 '19 at 10:01
  • @Nazeer_hanne ok so I can see a few potential issues with your setup, what are you trying to test? What's the purpose of the test? – James Jan 30 '19 at 10:06
  • @James, I had a test file from which i need to call another js file's private methods and properties. As you can see callMicroserive is a private function(non-exported) in which I need to stub post request – Nazeer_hanne Jan 30 '19 at 10:30
  • @James, I'm actually calling restAPI from test file, which is executing callMicroserive method internally as part of restAPI implementation. I want to stub all requests which are hitting remote servers. The purpose is to test and implement code coverage through unit test – Nazeer_hanne Jan 30 '19 at 10:32
  • @Nazeer_hanne please show a complete example of the test that's failing. Depending on the particular test you are trying to perform you may need to do more than just stubbing the function as stubbing the callback here means the rest of your code won't execute. – James Jan 30 '19 at 11:00

0 Answers0