Reputation: 11
I have an issue where I am trying to add a gravatar image to my code from the learn by examples book chapter 7 the site shows up fine but I am unable to pass the rspec spec test, I get the following errors:
1) UsersController should have the right title Failure/Error: get :show, :id => @user ActionController::RoutingError: No route matches {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:36:in `block (2 levels) in '
2) UsersController should include the user's name Failure/Error: get :show, :id => @user ActionController::RoutingError: No route matches {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:41:in `block (2 levels) in '
3) UsersController should have a profile image Failure/Error: get :show, :id => @user ActionController::RoutingError: No route matches {:id=>nil, :controller=>"users", :action=>"show"} # ./spec/controllers/users_controller_spec.rb:46:in `block (2 levels) in '
4) UsersController GET 'show' should be successful Failure/Error: get :show, :id => @user ActionView::Template::Error: undefined method
gravatar_image_tag' for #<#<Class:0x00000003dda0d8>:0x00000003dd31c0> # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/show.html.erb:7:in_app_views_users_show_html_erb___2751740854697998587_32401380__1353884467646085556' # ./spec/controllers/users_controller_spec.rb:13:in
block (3 levels) in '5) UsersController GET 'show' should find the right user Failure/Error: get :show, :id => @user ActionView::Template::Error: undefined method
gravatar_image_tag' for #<#<Class:0x00000003dda0d8>:0x00000002c7e140> # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/show.html.erb:7:in_app_views_users_show_html_erb___2751740854697998587_32401380__1353884467646085556' # ./spec/controllers/users_controller_spec.rb:18:in
block (3 levels) in '
To give you a little background I accidentally added the gravatar gem to the wrong area but I did change it back to the correct area
spec/controllers/users_controller_spec.rb
it "should have the right title" do
get :show, :id => @user
response.should have_selector("title", :content => @user.name)
end
it "should include the user's name" do
get :show, :id => @user
response.should have_selectori("h1", :content => @user.name)
end
it "should have a profile image" do
get :show, :id => @user
response.should have_selector("h1>img", :class => "gravatar")
end
end
app/controllers/Users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end
app/helpers/users_helper.rb
module UsersHelper
def gravatar_for(user, options = { :size => 50 })
gravatar_image_tag(user.email.downcase, :alt => user.name,
:class => 'gravatar',
:gravatar => options)
end
end
app/views/users/show.html.erb
<%= @user.name %>, <%= @user.email %>
<table class="profile" summary="Profile Information">
<tr>
<td class="main">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</td>
<td class="sidebar round">
<strong>Name</strong> <%= @user.name %><br />
<strong>URL</strong> <%= link_to user_path(@user), @user %>
</td>
</tr>
</table>
Upvotes: 1
Views: 560
Reputation: 27747
Your errors are clearly showing that :id => nil
which is not what is expected to be the case for the :action => :show
route.
This often happens in specs when you have not properly set up the @user
...
I bet if you changed one of your specs to this:
it "should have the right title" do
@user.id.should_not be_blank
get :show, :id => @user
response.should have_selector("title", :content => @user.name)
end
That it would fail on the very first line - saying something like "nil expected to not be blank".
The way to solve it would be to ensure that @user
is getting properly initialised with a user.
As @user861181 pointed out, this can be done in a before(:each)
block by setting up a factory. Alternatively (if you use fixtures), you can use:
before(:each) do
@user = users(:one)
end
Upvotes: 0
Reputation: 6394
For somebody who has the same error: make sure that in file users_controller_spec.rb lines:
before(:each) do
@user = Factory(:user)
end
is placed right after render_views otherwise user is not created for each method.
Upvotes: 1
Reputation: 4655
:id=>nil, maybe user is not logged in? and you need check it by using <% if current_user %>
Upvotes: 0