添加一个gravatar问题

时间:2011-06-28 01:34:10

标签: ruby-on-rails

我有一个问题,我试图通过示例书第7章向我的代码添加一个图像 该网站显示正常,但我无法通过rspec规范测试,我得到以下错误:

  

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

中的块(2级)      

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

     

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

     

4)UsersController GET'show'应该成功        失败/错误:get:show,:id =&gt; @用户        ::的ActionView ::模板错误:          未定义的方法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阻止(3级)'

     

5)UsersController GET'show'应该找到合适的用户        失败/错误:get:show,:id =&gt; @用户        ::的ActionView ::模板错误:          未定义的方法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阻止(3级)'

为了给你一点背景,我不小心将gravatar gem添加到了错误的区域,但我确实将它更改回了正确的区域

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

3 个答案:

答案 0 :(得分:1)

对于有相同错误的人:确保在文件users_controller_spec.rb中:

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

放在 render_views 之后,否则不会为每个方法创建用户。

答案 1 :(得分:0)

:id =&gt; nil,也许用户未登录?你需要使用&lt;%if current_user%&gt;来检查它。

答案 2 :(得分:0)

您的错误清楚地表明:id => nil:action => :show路线的情况不同。{/ p>

当您未正确设置@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

它会在第一行失败 - 说“nil预计不会空白”。

解决问题的方法是确保@user与用户正确初始化。

正如@ user861181指出的那样,这可以通过设置工厂在before(:each)块中完成。或者(如果您使用灯具),您可以使用:

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