Comments (10)
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.
Only way I got mocking working was to actually mock instantiated class itself.
from node-rest-client.
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.
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.
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.
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.
Thanks for the examples!
from node-rest-client.
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.
Thank you jtiai. Your solution worked very well.
from node-rest-client.
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)
- Vulernability in debug-2.2.0
- promise from http calls HOT 2
- TLS 1.2 Support HOT 1
- Support for retry HOT 1
- Cannot get examples to work HOT 2
- Handle JSON Hijacking prevention HOT 1
- xmlSerializer is not defined
- NPM Vulnerability HOT 2
- Problem behind a Proxy with Tunnel HOT 4
- Can I send a binary file with this?
- "node_modules\node-rest-client\lib\nrc-serializer-manager.js:122" HOT 1
- Result is not defined in nrc-parser-manager HOT 1
- Use a loop? HOT 1
- HTTPS: unable to verify the first certificate error HOT 2
- https encryption
- Content-Type header is case sensitive
- Cannot send multipart/form-data
- The git repository has been abandoned
- "Error: socket hang up" when using node-rest-client if follow-redirects Ver > 1.13.3 HOT 1
- Replace xml2js due to CVE-2023-0842
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-rest-client.