RSpec fails although it shouldn't

Hi all, i wrote a Rspec test for my controller and it fails although it should pass. Here is my Rspec test:

require 'spec_helper'

describe UsersController do   render_views

  describe "GET 'show'" do     it "returns http success" do       get 'show'       response.should be_success     end

    it "should have the right title" do       get 'show'       response.should have_selector("title",:content => " | User")     end   end

end

The test fails with this message:

Failures:

  1) UsersController GET 'show' returns http success      Failure/Error: get 'show'      ActionController::RoutingError:        No route matches {:controller=>"users", :action=>"show"}      # ./spec/controllers/users_controller_spec.rb:20:in `block (3 levels) in <top (required)>'

  2) UsersController GET 'show' should have the right title      Failure/Error: get 'show'      ActionController::RoutingError:        No route matches {:controller=>"users", :action=>"show"}      # ./spec/controllers/users_controller_spec.rb:25:in `block (3 levels) in <top (required)>'

The controller is already created also the view. I got the method show in the users controller. I also added the resources :users to my routes.rb. I am able to reach the page without problems. I also checked the rake routes and they are available for this test so it should go green.

Maybe someone see an error and can help me out with this. It would be great.

Thanks and regards

Greg

Don't you need to specify an id there? Or are you thinking "index" (show *all*)?

-Dave

How to do that? I just started to learn how write tests for the application.      describe "GET 'show'" do                it "returns http success" do                        get 'show/1'                        response.should be_success          end

OR can i use a wildcard?

describe "GET 'show'" do                it "returns http success" do                        get 'show/:id'                        response.should be_success          end

What makes me pondering is that rake routes is shows that the route is available so how can the id be the problem that the test fails?! Sorry if this question is stupid but I'm still in the learning stage

users GET /users(.:format) {:action=>"index", :controller=>"users"}                   POST /users(.:format) {:action=>"create", :controller=>"users"}          new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}         edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}              user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}                   PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}                   DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}

Don't you need to specify an id there?

How to do that? I just started to learn how write tests for the application.

I recommend you read:

  Testing Rails Applications — Ruby on Rails Guides

(and then all the other assorted Rails Guides). To get you started, it contains this snippet that might be helpful:

  Example: Calling the :show action, passing an id of 12 as the params and setting a user_id of 5 in the session:     get(:show, {'id' => "12"}, {'user_id' => 5})

  Another example: Calling the :view action, passing an id of 12 as the params, this time with no session, but with a flash message.     get(:view, {'id' => '12'}, nil, {'message' => 'booya!'})

So for your purposes, I'd think that:

    get :show, :id => '1'

might do fine, assuming that there is indeed a user with ID 1 in your test database.

What makes me pondering is that rake routes is shows that the route is available

Right, but if you look at that line:

user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}

you see ":id", which means you need to supply that somehow. (The :format part is in parens, meaning it's optional.)

Sorry if this question is stupid but I'm still in the learning stage

No problem, we all started there too. :slight_smile: Kudos to you for testing at all, many people skip that entirely! :frowning:

-Dave

Don’t you need to specify an id there?

How to do that? I just started to learn how write tests for the application.

I recommend you read:

http://guides.rubyonrails.org/testing.html

(and then all the other assorted Rails Guides). To get you started, it contains this snippet that might be helpful:

Example: Calling the :show action, passing an id of 12 as the params and setting a user_id of 5 in the session: get(:show, {‘id’ => “12”}, {‘user_id’ => 5})

Another example: Calling the :view action, passing an id of 12 as the params, this time with no session, but with a flash message. get(:view, {‘id’ => ‘12’}, nil, {‘message’ => ‘booya!’})

So for your purposes, I’d think that:

get :show, :id => '1'

might do fine, assuming that there is indeed a user with ID 1 in your test database.

Thanks for the link i will start reading the tutorials. For now your posted proposal worked, after i added some data to the test database (is easy to forget)

What makes me pondering is that rake routes is shows that the route is available

Right, but if you look at that line:

user GET /users/:id(.:format) {:action=>“show”, :controller=>“users”}

you see “:id”, which means you need to supply that somehow. (The :format part is in parens, meaning it’s optional.)

You are right my method will only work for static routes or routes like index, new

Sorry if this question is stupid but I’m still in the learning stage

No problem, we all started there too. :slight_smile: Kudos to you for testing at all, many people skip that entirely! :frowning:

Nice to hear that :slight_smile:

Thanks for your help

Regards Greg

-Dave

– LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages. Where: Northern Virginia, Washington DC (near Orange Line), and remote work. See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence). Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)

– You received this message because you are subscribed to the Google Groups “Ruby on Rails: Talk” group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.