How to mock a request in ruby on rails - ruby-on-rails

How to mock a request in ruby on rails
I am making a call to multiple external sites via API and facing lot of failures in the response due to net read timeout error and parameters errror , run time errors, etc.
I want to mock the request before making a original call to the API. So that i can avoid more failures in my app.
Can any one help me ?

Try using the VCR gem, it records HTTP request for you, the first time you run your tests. For subsequent tests VCR uses the previously recorded HTTP response.
This should solve your timeout errors and allow you to work with external API's more easily.
Checkout these resources for more info:
https://www.relishapp.com/vcr/vcr/docs
http://natashatherobot.com/vcr-gem-rails-rspec/

Related

Stripe Exception double rendering using stripe gem

I am using Stripe gem in my rails application, it's working fine in the development environment but in my production environment getting an exception.
Stripe::APIError: (Status 409) with message There is currently another in-progress request using this Idempotent Key (that probably means you submitted twice, and the other request is still going through).
How can I rescue this or handle this exception?
Any help would be appreciated.
The retry logic is supposed to be for when your application doesn't know Stripe's response, primarily during network issues likes timeouts. In this case your server received a response from stripe and one of two things could be happening. Either you're retring the same event that is currently in progress /or/ the event in progress is actually different than the one you're trying but given some issue in your application stack you actually chose the same indempotency token for two api requests.
For further details please read this link :- https://stripe.com/docs/api/idempotent_requests

How to store only the latest API response in Rails VCR Testing?

We are using the Ruby VCR library to mock and replay responses from various APIs.
In one function that we have, our code continuously polls an API endpoint until the response is ready. The VCR Cassette is storing each of these polling requests, with the "Not Ready" response. At the very end of the cassette, the actual response is stored.
How can I configure VCR to store only the last and actual response? I can go into the YML file and manually delete the polling requests, but I would prefer an automated way since I would have to delete these requests on every new cassette recording.
The main reason why I want to delete these polling requests is due to the time it takes to run our tests (we have a lengthy sleep in our code between each unsuccessful poll).

How to test a Rails HTTP request to a Sinatra app?

Consider a Rails app that hits a (Sinatra app) API being developed separately from the Rails app. I want to test an API call from within the Rails tests.
The API code:
post '/foo/create' do
...
I created a mock, but that doesn't make sense because it is just a copy of the API file. That stinks.
It is possible to require the API file in the test. But how to call it from RSpec? There is no route in the Rails app for it.
One option is to start the API and make the HTTP call from the Rails test, but this is smelly because:
You have to start the API server to run the Rails tests
Why should a Rails test make a HTTP request? Rack::Test simulates this.
I don't think this will work because the apps have different test databases, but share the same production database.
EDIT: The point of the test is that the API call creates records that the Rails app is expecting. So the Rails app needs to test the state of the database after the API call is made.
Well. The perfect answer for you is a gem to mock the answer like webmock. It will fake a response when acessing that url, so on the test your app will make the requisition as it was for real, only that before it hits the web, it will hit your mock and respond with the desired answer.

How to get response headers and time using capybara-webkit

How to get response of seperate ajax requests in a web page using capybara-webkit?
Is there any particular method available to capture response time of each request?
Note:
Am using capybara with rspec.
For eg: i have 3 Ajax requests in a web page. I need to get separate response time of each request and the response time of entire web page.
Thanks,
Priya
Generally it's not possible since webserver is running in the different process but you could create a custom rack middle-ware and dump all responses to the separate log file. For the beginning you could implement technique described here: https://gist.github.com/2975611 and for dumping headers you could use some code snippets from http://rack.rubyforge.org/doc/Rack/ContentLength.html

How to simulate external APIs?

We call many different external APIs in our system and now I'm looking for a system I can use to simulate those APIs so we can test ours in the Staging and Development environments?
Our application is written in Ruby on Rails 3.0 but since all the API calls to and from it are over HTTP there is no language dependency.
VCR will record the actual input from the webservice and then replay that feedback from then on.
To simulate it completely, you can use fakeweb. You'll record output to a file and have it sent back to your application.
This something called test mocking/stubbing and is a common practice. Basically you override the response code of the API call to return data w/o actually doing the HTTP request. Just search it for more details.

Resources