2011-06-30 37 views
0

我对问题通过实例书第7章,其中在第一章中RSpec的规范得到这些错误信息的末尾了解Rails的故障/错误:得到:秀:ID => @user

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

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

3)UsersController应该有一个profile image失败/错误:get:show,:id => @用户ActionController :: RoutingError:没有路由匹配{:id => nil,:controller =>“users”,:action =>“show”}#./spec/controllers/users_controller_spec.rb:46:in`block(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> 

规格/工厂。 .rb

Factory.define :user do |user| 
user.name "Michael Hartl" 
user.email "[email protected]" 
user.password "foobar" 
user.password_confirmation "foobar" 
end 

回答

0

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

+0

那么如何添加@user?另外你对整个测试意味着什么? – mercxi

+0

我也加了这个描述“GET”show'“做 之前(:each)做 @user = Factory(:user) end 它应该是成功的do get:show,:id => @用户 response.should be_success 结束 它 “应该找到正确的用户” 做 得到:秀:ID => @user 受让人(:用户)。应该== @user 结束 – mercxi

+0

@mercxi:请不要把你的测试放在评论中。编辑你的问题,并把它放在那里,这样我们就可以看到它。 –

1

我想你需要

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

甚至只是

get :show, :id => '1' 

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