2011-06-28 158 views
1

我有一個問題,我試圖添加一個gravatar圖像到我的代碼從學習示例書第7章 該網站顯示正常,但我無法通過rspec規範測試,我得到以下錯誤:添加gravatar問題

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 '

爲了給你提供一些背景,我不小心添加的gravatar寶石錯誤的領域,但我沒有改回正確的區域

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

回答

0

: ID =>無,也許用戶沒有登錄?你需要使用<%檢查,如果CURRENT_USER%>

+0

我不認爲是這樣的話 – mercxi

+0

低於軌控制檯還通過了代碼,如果它可以幫助 – mercxi

+0

$軌控制檯 >>用戶= User.first >> user.update_attributes(:電子郵件=> 「[email protected]」, > :密碼=> 「foobar的」, > :password_confirmation => 「foobar的」) =>真 – mercxi

1

對於有人誰擁有了同樣的錯誤:請確保文件users_controller_spec.rb行:

 before(:each) do 
     @user = Factory(:user) 
     end 

是右後render_views放置,否則用戶不會爲每種方法創建。

0

您的錯誤清楚地顯示:id => nil這不是:action => :show路線預期的情況。

這通常發生在當你有沒有正確設置@user ... 我敢打賭,如果你改變了你的規格之一,這個規格:

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 

,它將無法在第一行 - 說「無預期的不是空白」。

解決這個問題的方法是確保@user正在被用戶正確初始化。

正如@ user861181指出的那樣,可以通過設置工廠在before(:each)塊中完成。或者(如果你使用夾具),你可以使用:

before(:each) do 
    @user = users(:one) 
    end