0

I have implement node js code to call HTTP API's using request module. For get, post, put, delete, I have directly called request() of require module,

var sendRequest = function(req, callback) {
    request(req, function(err, res) {
        if (err) {
            callback(err, null, res);
        } else {
            callback(null, res);
        }
    }
}

I want to invoke sendRequest() but mock the response of request(). Any pointers for this.

  • You can use sinon like described here -> http://stackoverflow.com/questions/20050507/creating-request-stub-with-sinon-in-mocha/32868852#32868852 – Bianca Apr 22 '16 at 06:49
  • I went through the link, but in that case we will have to write independent get(), put(), etc. As you can check above I have implemented on request() which takes care of all the method types. I need to mock that particulare request(). – Priyanka Ware Apr 22 '16 at 07:53
  • @PriyankaWare did you find the answer? – visingh Apr 19 '20 at 07:57

2 Answers2

2

One way to get it stubbed without anything but sinon, is to stub the inner class of Request, which conveniently offers its prototype, like this

stub = sinon.stub(request.Request.prototype, "init");
stub.callsFake( options => options.callback(null, {statusCode: 200}, "faked response"));

Where options in the callsFake are processed by the request function and the passed into the Request.prototype.init.

Now your callback parameters, are the ones expected in your var sendRequest = function(req, callback) {...} function

Boris Z.
  • 21
  • 2
0

One of the many options is to use proxyquire to stub the request module:

var proxyquire = require('proxyquire')
  , assert = require('assert')
  , requestStub = function (req, callback) { callback(req); };

// assuming you export the sendRequest from that file
var sendRequest = proxyquire('./sendRequest', { 'request': pathStub });

describe('sendRequest', function() {
    it('request', function (done) {
        var someReq = {a: 'b'};
        sendRequest(someReq, function (req) {
            assert.equal(someReq, req);
            done();
        });
    });
});
Vasyl Boroviak
  • 5,959
  • 5
  • 51
  • 70
  • Thank for your reply, but what I observed is to make it working I will have to export sendRequest(). In my case, sendRequest is prototype method and I cannot export it. – Priyanka Ware Apr 25 '16 at 05:06
  • @PriyankaWare you're always welcome. How much harm will it cause if you export the function? Can you move it to a separate private file maybe? – Vasyl Boroviak Apr 25 '16 at 05:26
  • It will not cause any harm for sure. But that is the methodology we are following to define all methods as prototype methods. If it is the only option I can go for that change, else it will be great if we get some alternative for that. – Priyanka Ware Apr 25 '16 at 05:54
  • @PriyankaWare by "prototype method" you mean a method attached to a JS class-like function? – Vasyl Boroviak Apr 25 '16 at 05:58
  • Yes exactly. In my JS file I am creating DataSource and attaching few prototype methods to it and in my test file I am creating new Data Source requiring that JS file and the invoking the prototype methods. – Priyanka Ware Apr 25 '16 at 06:03
  • @PriyankaWare why don't you just proxyquire the request module itself? – Vasyl Boroviak Apr 25 '16 at 06:25