Reputation: 11794
I have a site that displays a section with stack overflow queries related to a post. To get these, it uses the SO API to search SO for posts similar to the title of the post on my site.
My question is, where should I put this method? It's directly related to posts in that it uses a post's title to make the API call. But it's also directly related to the controller in that it returns data that must be passed on to the view. I doubt I'd want to put code for even a simple API call inside of an actual view. Maybe I should put it in a library/helper method?
Also, once I've decided where to put the method, how should I test it without having to call the API every time? I'm trying to read up on stubs but I'm getting confused. It'd be nice to have an example in a context like this and build my knowledge from there.
Upvotes: 1
Views: 91
Reputation: 859
You can use gem for stub http-requests.
I used for it webmock which has integration with rspec
, test/unit
or minitest
Upvotes: 2
Reputation: 559
In my opinion, you should put it in a new model (without inheriting from ActiveRecord). Then make the controller call it to get the data and pass it on to the view. If you'd like to test this model - you can use "fakeweb" which is a gem that allows you to mock http requests with your own results.
https://github.com/chrisk/fakeweb
Upvotes: 1