失败/错误:{visit root_path}之前

时间:2014-07-04 02:04:14

标签: ruby-on-rails ruby ruby-on-rails-3 rspec tdd

您好,我目前正在迈克尔哈特尔教程的第10章,这个教程的这一小部分似乎无法通过。

当我运行bundle exec rspec spec/requests/micropost_pages_spec.rb时,它会一直失败。

附件是失败:

Failures:                                                                                                                                

  1) Micropost pages micropost creation with invalid information should not create a micropost                                           
     Failure/Error: before { visit root_path }                                                                                           
     ActionView::Template::Error:                                                                                                        
       wrong number of arguments (2 for 1)                                                                                               
     # ./app/helpers/users_helper.rb:4:in `gravatar_for'                                                                                 
     # ./app/views/shared/_user_info.html.erb:1:in `_app_views_shared__user_info_html_erb__742172174590561382_70297875133320'            
     # ./app/views/static_pages/home.html.erb:5:in `_app_views_static_pages_home_html_erb___3080353205945370821_70297875083140'          
     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'                                              

  2) Micropost pages micropost creation with invalid information error messages                                                          
     Failure/Error: before { visit root_path }                                                                                           
     ActionView::Template::Error:                                                                                                        
       wrong number of arguments (2 for 1)                                                                                               
     # ./app/helpers/users_helper.rb:4:in `gravatar_for'                                                                                 
     # ./app/views/shared/_user_info.html.erb:1:in `_app_views_shared__user_info_html_erb__742172174590561382_70297875133320'            
     # ./app/views/static_pages/home.html.erb:5:in `_app_views_static_pages_home_html_erb___3080353205945370821_70297875083140'          
     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'                                              

  3) Micropost pages micropost creation with valid information should create a micropost                                                 
     Failure/Error: before { visit root_path }                                                                                           
     ActionView::Template::Error:                                                                                                        
       wrong number of arguments (2 for 1)                                                                                               
     # ./app/helpers/users_helper.rb:4:in `gravatar_for'                                                                                 
     # ./app/views/shared/_user_info.html.erb:1:in `_app_views_shared__user_info_html_erb__742172174590561382_70297875133320'            
     # ./app/views/static_pages/home.html.erb:5:in `_app_views_static_pages_home_html_erb___3080353205945370821_70297875083140'          
     # ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'                                              

附件是app / helpers / users_helpers.rb:

module UsersHelper

  # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user)
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end
end

附件是_user_info.html.erb:

<%= link_to gravatar_for(current_user, size: 52), current_user %>
<h1>
  <%= current_user.name %>
</h1>
<span>
  <%= link_to "view my profile", current_user %>
</span>
<span>
  <%= pluralize(current_user.microposts.count, "micropost") %>
</span>

附件是home.html.erb:

<% if signed_in? %>
  <div class="row">
    <aside class="span4">
      <section>
        <%= render 'shared/user_info' %>
      </section>
      <section>
        <%= render 'shared/micropost_form' %>
      </section>
    </aside>
  </div>
<% else %>
  <div class="center hero-unit">
    <h1>Welcome to the Sample App</h1>

    <h2>
      This is the home page for the
      <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
      sample application.
    </h2>

    <%= link_to "Sign up now!", signup_path,
                                class: "btn btn-large btn-primary" %>
  </div>

  <%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
<% end %>

附件是micropost_pages_spec.rb:

require 'spec_helper'

describe "Micropost pages" do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user }

  describe "micropost creation" do
    before { visit root_path }

    describe "with invalid information" do

      it "should not create a micropost" do
        expect { click_button "Post" }.not_to change(Micropost, :count)
      end

      describe "error messages" do
        before { click_button "Post" }
        it { should have_content('error') }
      end
    end

    describe "with valid information" do

      before { fill_in 'micropost_content', with: "Lorem ipsum" }
      it "should create a micropost" do
        expect { click_button "Post" }.to change(Micropost, :count).by(1)
      end
    end
  end
end

1 个答案:

答案 0 :(得分:1)

def gravatar_for(user)
  ...

方法签名指定一个参数。但是在视图中,使用两个参数调用该方法:

link_to gravatar_for(current_user, size: 52)

因此错误wrong number of arguments (2 for 1)

您可能需要重新定义方法签名,例如:

def gravatar_for(user, options={})