How to test an RSS feed, with cucumber or rspec - ruby-on-rails

Anyone have any tips on how to test an rss feed with cucumber (preference) or rspec?
Note, I am currently working on a Rails 3 application with a blog which I expose as an rss feed.
I would like to set up a test to ensure that it remains well formatted and consumable.
Thanks!
Jonathan

To test an RSS feed with RSpec you can use :format => "rss" in your controller tests, i.e. you can simple use something like this:
describe "GET RSS feed" do
it "returns an RSS feed" do
get :index, :format => "rss"
response.should be_success
response.should render_template("posts/index")
response.content_type.should eq("application/rss+xml")
end
end
Using Test::Unit, you would write
def test_get_rss_feed
get :index, :format => "rss"
assert_response :success
assert_template "posts/index"
assert_equal 'application/rss+xml', #response.content_type
end

You could find some ideas by looking at the specs for an existing RSS gem, such as feedzirra.

Related

Ruby on Rails difference between get :home and static_pages_home_url

I am reading Ruby on Rails Tutorial (Ch 3.3.2) by Michael Hartl.
I am currently making a failing test(RED) However, I just noticed that his static_pages_controller_test.rb has
test "should get home" do
get :home
assert_response :success
end
and mine has although I followed everything he did.
test "should get home" do
get static_pages_home_url
assert_response :success
end
It seems like get :home is the same as get static_pages_home_url.
Is there any difference?
It does the same in this situation, but not in all situations.
since get :home is inside of your static_pages_controller_test.rb it looks in that controller automatically and finds the home method.
get static_pages_home_url is more dynamical. It looks for the home url inside of your static_pages_controller.
So you can call get_static_pages_home_url in tests on different locations but you can't with get :home.

functional test to confirm custom route with devise

I have a custom route for devise:
devise_scope :user do
get '/login' => "devise/sessions#new", :as => :new_user_session
get '/logout' => 'devise/sessions#destroy',
...
I want to make sure that when the request /login is called, that it gets correctly routed to devise and that the response is successful.
How do you test for custom routing and a successful request?
If you're using the built-in TestUnit to test, check out the Rails Guide on testing controllers.
If you're using Rspec, check out the Rspec Github Page for information on testing controllers.
It depends on you testing suite, personally I use RSpec + Capybara for my Rails projects.
If you don't have a testing suite yet I highly recommend this guide.
http://everydayrails.com/2012/03/12/testing-series-intro.html
In this particular instance, I'd say you want two tests. One for the routing and one for a successful request. Using rspec, this will look something like:
#spec/routing/devise_routing_spec.rb
require 'spec_helper'
describe "Devise Routes" do
it "should route the login correctly" do
{:get => "login"}.should route_to(controller: "devise/sessions", action: "new")
end
end
and
#spec/controllers/devise/session_controller_spec.rb
require 'spec_helper'
describe Devise::Sessions do
it "should be successful with a login request" do
get "new"
response.should be_success
end
end

Rails custom route not found when testing, but works for curl

In config/routes.rb:
match 'app_config/:version' => "application#appconfig"
test/functional/application_controller_test.rb:
require 'test_helper'
class ApplicationControllerTest < ActionController::TestCase
test "app config" do
get "/app_config/v2"
assert_response :success
end
end
rake test:
1) Error:
test_app_config(ApplicationControllerTest):
ActionController::RoutingError: No route matches {:controller=>"application", :action=>"/app_config/v2"}
but $ curl localhost:3000/app_config/v2 works, and returns the response I expect, and rake routes shows the route as expected. Any idea what's going on, or how to investigate further? This is basically the only code in the project.
The get method does not take a route. It takes an action name and a parameter's hash.
If you want to test the action, your tests should look like this:
test "for success" do
get :appconfig, :version => "v2"
assert_response :success
end
If you want to test routing there is a tutorial here in the documentation.
test "should route to appconfig" do
assert_routing '/app_config/v2', { :controller => "application", :action => "appconfig", :version => "v2" }
end

rspec - customised route

I have simplified an issue I am having to the following
I have a route that looks like this
namespace :client do
resources :thing, :only => [:index]
end
and the simple rspec test
describe Client::ThingController do
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end
However what I'd like to do is alter the url I use to access the resource from
/client/thing.json
to
/api/client/v1/thing.json
1) How do I update my routes and rspec?
If I then wanted to parameterise the uri such that I could extract the api_version
/api/client/[api_version]/thing.json
2) How would this effect my routes and simple rspec test?
I'd previously tried what #Mori had suggested before I had posted but without having the multiple routes as proposed, I should have mentioned that in the original post.
What I eventually got that worked is:
1) In routes.rb I added
match "api/client/:api_version/thing" => 'client/thing#index'
NOTE: The missing leading '/' ie match "/api/... => match "api/... it seems to make all the difference to rspec.
2) And in my rspec thing_controller_spec.rb I now have this
describe Client::ThingController do
describe "GET 'index'" do
it "returns http success" do
get 'index', :api_version => 'v1'
response.should be_success
end
end
end
I was close before but it was the leading '/' in routes.rb that broke me even though I could navigate to the url in a browser.

Why doesn't this Rails Rspec Test Work?

I create all the routes of my program manually and so do with my rspec tests of course. Generally, my routes and tests work fine, but i have a problem with the test for my characters controller. The route is :
scope :path => '/characters', :controller => :characters do
get '/' => :show, :as => 'user_character'
end
The /characters works fine when tested with my browser. Everything seems fine. But, the test :
require 'spec_helper'
require 'devise/test_helpers'
describe CharactersController do
login_user
describe "when it GETS 'show'" do
it "should render the template and be successful" do
get :show
response.should render_template(:show)
response.should be_success
end
end
end
Fails with the error :
1) CharactersController when it GETS 'show' should render the template and be successful
Failure/Error: get :show
ActionController::RoutingError:
No route matches {:controller=>"characters", :action=>"show"}
# ./spec/controllers/characters_controller_spec.rb:9
All my controllers have similar tests that work fine. Why does this not work ?
IMPORTANT EDIT :
Just saw that if i turn Spork off, the test passes ! Why is this happening ? Does Spork need to be restarted every time a new test is added ?
You have to restart spork when changing routes.
Or put this in your spec_helper.rb:
Spork.each_run do
ApplicationName::Application.reload_routes!
end
See also "Speedy Test Iterations for Rails 3 with Spork and Guard"

Resources