Failure/Error: get :show, :id => @user
I have an issue on the Learn Rails by Example book Chapter 7 where at the end of the chapter I get these error messages in the rspec spec
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 '
Below is all relevant code that I have done,
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
<%= @开发者_C百科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>
spec/factories.rb
Factory.define :user do |user|
user.name "Michael Hartl"
user.email "mhartl@example.com"
user.password "foobar"
user.password_confirmation "foobar"
end
I think you needed
get :show, :id => @user.id
or even just
get :show, :id => '1'
Instead you were using the entire object as a parameter.
This strongly indicates you didn't set up the @user
instance variable in before
block for these tests. I would say that was for certain if I could see the whole test, but it certainly looks like it.
精彩评论