Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stub an external API for testing

I have a project that makes some calls to Twitter's API from the node.js server that I would like to test. However, I don't want to test the Twitter API (OAuth and the actual API I'm calling), so I thought it would be best to stub it out.

I found sinon.js that supports this supposedly. I have not found an example of how to do this and was wondering if this has been done before. And if so, seeing some sample code would really help.

like image 629
staackuser2 Avatar asked Dec 12 '25 11:12

staackuser2


1 Answers

What are you doing to call the API?

Imagine you use a method called api.call('url', function (error, response) {...}):

you can "fake" the callback with sinon, gently or whatever you like.

Example using gently:

gently.expect(api, 'call', function (url, callback) {
  assert.equal(url, 'http://api.twitter.com/...');
  callback(Error('Fake error'), null);
});
like image 119
masylum Avatar answered Dec 14 '25 06:12

masylum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!