1
votes

I'm getting unexpected errors when running some Rspec tests. They are

1) PeopleController redirects when loading root should redirect to the splash page Failure/Error: get '/'

ActionController::UrlGenerationError: No route matches {:action=>"/", :controller=>"people"}

...

2) PeopleController redirects when loading /people/show should redirect to the base person path Failure/Error: get '/show' #/show

ActionController::UrlGenerationError: No route matches {:action=>"/show", :controller=>"people"}

I don't understand why Rspec can't find the routes.

From the controller, people_controller.rb:

class PeopleController < ApplicationController

...

    def show
        redirect_to people_path
    end

    def index
        @people = Person.all
    end

...

From the Rspec file people_controller_spec.rb:

describe PeopleController do
    describe "redirects" do
        context "when loading root" do
            it "should redirect to the temp page" do
                get '/'
                last_response.should be_redirect
                follow_redirect!
                last_request.url.should include('/temp')
            end
        end

        context "when loading /people/show" do
            it "should redirect to the base people path" do
                get '/people/show'
                last_response.should be_redirect
                follow_redirect!
                last_request.url.should include('/people')
            end
        end
    end
end

And my routes:

$ rake routes
      Prefix Verb   URI Pattern                  Controller#Action
...
      person GET    /people/:id(.:format)        people#show
...
        root GET    /                            redirect(301, /temp)

routes.rb:

Rails.application.routes.draw do
  resources :temp
  resources :people

  # map '/' to be a redirect to '/temp'
  root :to => redirect('/temp')
end

What am I missing to get the routes from test to match up? I could see the root test not working because it's not technically handled by the People controller (I tried putting it in as a sanity test and only made myself more confused) but the /show failure really makes no sense to me.

1

1 Answers

3
votes

Inside a controller test the method get takes an action argument, not a path. If the resource is a member (as opposed to a collection), you must also specify the id parameter, so:

get :show, id: 1

will call the #show action on an instance of PeopleController, with a params hash including {id: '1'}.

This is described in more detail in the Guide to Testing Rails Applications.