GithubHelp home page GithubHelp logo

Comments (10)

flosky avatar flosky commented on July 19, 2024

Hi, I'm having trouble unit testin my code with node-rest-client too.

I want to mock a GET rest call, so I try this in my test file using sinon stubs:

var Client = require('node-rest-client').Client;
var client = new Client();

sinon.stub(client, 'get');

But when I run the tests the get call is still executed to the real url and the stub is not used. I suppose that is because I create a new client with new. So I would have to create the stub on the Client Object, but I don't seem to be able to access the method 'get' then.

So what I would like to do is sth like this:

sinon.stub(Client, 'get');

or

sinon.stub(Client.prototype, 'get');

Could you please help me with created a stub Object that I can use to run my tests without making any real REST calls?

Thank
Flosky

from node-rest-client.

jtiai avatar jtiai commented on July 19, 2024

Only way I got mocking working was to actually mock instantiated class itself.

from node-rest-client.

anton-johansson avatar anton-johansson commented on July 19, 2024

Yes, it's the instansiated class I want to mock. But I'm not sure how to do it best, considering the promises, such as .on('error', function(error) { ... });

from node-rest-client.

jtiai avatar jtiai commented on July 19, 2024

Problem is that node rest client uses class method generation in the class constructor - which causes the issue that all methods are always recreated per instance - thus making mocking them impossbile through prototype.

My suggestion would be moving (at least) all methods (get/post/put/patch/delete) as a prototype definition from constructor.

So this is how I did it for time being.

First I created a small stub that mimics object returned from call. Inner on is required for error handler.

var restResponseMock = function(data, statusCode) {
    return function(url, opts, cb) {
        cb(data, {statusCode: statusCode || 200});
        return {
           on: function() {
           }
        };
    }
}

And then in my test case:

    var clientGetMock = sinon.stub(rest_client, 'get', restResponseMock({data: 'FooBar'}, 200));

Note that client must be actual instance you use in your code, so either you declare it at module level and export it (which I did):

var Client = require('node-rest-client').Client;
var rest_client = new Client();

/* 
 * <some code you want to test here>
*/

module.exports.rest_client = rest_client; // This is needed for tests

from node-rest-client.

anton-johansson avatar anton-johansson commented on July 19, 2024

Looks good! And if you would want to mock the on('error') flow, how would you do? I would want it to call my callback for the 'error' event, to simulate this flow.

from node-rest-client.

jtiai avatar jtiai commented on July 19, 2024

I used EventEmitter and process.nextTick to emit error event - not sure is that correct way to do it...

var restErrorMock = function(data) {
    return function(url, opts, cb) {
        var eventEmitter = new events.EventEmitter();
        process.nextTick(function() {eventEmitter.emit('error', data)});
        return eventEmitter;
    };
}

And in a test case:

var clientGetMock = sinon.stub(rest_client, 'get', restErrorMock(new Error("DARN!")));

from node-rest-client.

anton-johansson avatar anton-johansson commented on July 19, 2024

Thanks for the examples!

from node-rest-client.

haribabupuppala avatar haribabupuppala commented on July 19, 2024

Hi Anton Johansson

I am not able to test using the above mentioned approach.

Kindly request you to please share some sample test case that you tested for node-rest-client.

This will really help me to resolve my issues,

Thanks
Hari.

from node-rest-client.

pradeep20mce avatar pradeep20mce commented on July 19, 2024

Thank you jtiai. Your solution worked very well.

from node-rest-client.

praveen-uppula avatar praveen-uppula commented on July 19, 2024

Hi,
I am also not able to test using the above approach. Can any one Please help me on how to mock node-rest-client request.

from node-rest-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.