I make API calls when create a user but I don't want do this calls on test environment.
What do you do with on this situations? I have external APIs like Mailchimp and internal APIs to communicate with others apps, how do you to test or not this APIs?
Mocking API is the purpose of fakeweb presented in this railscast.
I think you need to mock your API calls then using some mocking libraries for Ruby.
Related
I'm writing tests for my Rails app. My app communicates with an external API that processes customer payments, specifically BrainTree. Now, I want to make sure my app's class that communicates with BrainTree works properly e.g. that it submits user information and other parameters to BrainTree correctly. Making the goal to only test that BrainTree and my App are communicating properly.
One thing to note, is that BrainTree has a sandbox account. To test my class, should I:
Write a feature test using something like Capybara and Rspec and test it from a user's perspective e.g. user logs in, fills out form, submits payment etc.
Write a request spec that just submits the required information and examines the return values. This is what I would prefer but is tricky since BrainTree requires js, and I am not sure I can do have js in a request spec without monkey patching Rspec, which I'd rather not do since I am still fairly new to Rspec and testing in general.
Write both feature and request specs
Write a completely different type of test
I have a feature test in place, but it seems cumbersome to use to just test an external API since it needs to open a browser, fill out forms, etc. in my feature spec I'd rather stub the external API and test the API as a unit test. A request spec seems more efficient but the js requirement seems like a roadblock.
Is there a Best Practice to what I should do in my scenario above?
In general, you don't typically want to write tests only for an external service, but instead for your own code that tests against the responses you receive.
The best way I've found to stub a response from an external API is the VCR gem. This will get a legitimate response and save it for use in future runs. You can erase the stored response occasionally to ensure continued functionality.
Another approach to testing this is to use a fake service that mimics the API. Fake Braintree Gem provides such functionality and I've used this with a mix of VCR for other tests to ensure correct functionality. There's many other approaches but you can test to see which one fits your needs
spray-swagger appears to be a good way to auto-generate documentation as well as client stubs for a REST API. Is there a way to automatically test these APIs using specs2?
My project is a rails app that extends some third party API. A lot of the requests rely on third party API calls. How should I test these cases in rspec? Should I use VCR and actually just hit the third party (then mock future requests)? Or should I just download the payload into a fixture manually and stub requests with webmock and find a way to bypass the oauth process? Are the better solutions?
Note that it uses OAuth, but I don't use omniauth.
Sometimes the API limits me to fetching N records at a time, so I have to paginate them. There could be instances where I'm making 25 requests just to get the data I need, but this is mostly for the sync rake tasks.
There is no need to download a payload manually, as this is exactly what VCR does for you. VCR creates a yaml fixture, that it uses for all future requests.
My app interfaces with Google Maps for geocoding and Stripe for payments. Using VCR I've mocked all requests to these services, which works great. But the libraries for both are still being loaded in javascript_include_tags. What's the best way to deal with this so that integration tests can run completely disconnected from the internet?
If you want to stub the javascript, then why record the interactions in the first place? Am I missing something?
I'm pretty sure geocoding from a test is a violation of the google maps ToS. I think what you want to do is stub the geocoding itself.
Have never used Stripe, but I might take this approach: add a method in a spec helper to cache the Stripe javascript locally, i.e., when a connection is available, download the Stripe javascript. Then in your view load the javascript locally if Rails.env.test?.
For anyone looking for a solution these days, there is a nice gem called Puffing Billy.
Similarly to how VCR allows you to record and replay external requests made by your server code, this library allows you to do the same for external requests made by your client code.
I'm using rails 3 and experimenting with the facebook graph api. I'm trying to do things in a TDD/BDD way but I'm not sure how to test my calls to the api. Do I simply wrap the facebook api methods into stubs and return mock objects?
I'm actually faced with a similar problem. I'm looking at https://github.com/mislav/fakeweb as a way of mocking out http responses. Haven't used it for real yet, but based on my preliminary experiments I think it'll work for my purposes. Maybe it'll work for yours too.
i've recently released a utility to help with this. it's a standalone implementation of Facebook's FQL and Graph API that you can run on your development machine.
http://code.google.com/p/mockfacebook/