失败/错误:get:show,:id => @用户

时间:2011-06-30 23:24:04

标签: ruby-on-rails-3

我在通过示例学习Rails 一书中遇到了问题第7章在本章末尾我在rspec规范中得到了这些错误消息

1)UsersController应该有正确的标题失败/错误:get:show,:id => @user ActionController :: RoutingError:没有路由匹配{:id => nil,:controller =>“users”,:action =>“show”}#。/ spec / control / users_controller_spec.rb:36:in '阻止(2级)'

2)UsersController应该包含用户名失败/错误:get:show,:id => @user ActionController :: RoutingError:没有路由匹配{:id => nil,:controller =>“users”,:action =>“show”}#。/ spec / control / users_controller_spec.rb:41:in '阻止(2级)'

3)UsersController应该有个人资料图片失败/错误:get:show,:id => @user ActionController :: RoutingError:没有路由匹配{:id => nil,:controller =>“users”,:action =>“show”}#。/ spec / control / users_controller_spec.rb:46:in '阻止(2级)'

以下是我所做的所有相关代码,

规格/控制器/ 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

应用程序/控制器/ Users_controller.rb

class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end

应用程序/助手/ 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

应用程序/视图/用户/ 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>

规格/ factories.rb

Factory.define :user do |user|
user.name   "Michael Hartl"
user.email  "mhartl@example.com"
user.password   "foobar"
user.password_confirmation  "foobar"
end

2 个答案:

答案 0 :(得分:1)

我认为你需要

get :show, :id => @user.id

甚至只是

get :show, :id => '1'

相反,您使用整个对象作为参数。

答案 1 :(得分:0)

这强烈表明您没有在@user块中为这些测试设置before实例变量。我会说这是肯定的,如果我能看到整个测试,但它看起来确实如此。