2012-10-16 30 views
1

当运行一些测试使用RSpec,我得到这个错误(多次):这个Rails错误(参数的错误数量)告诉我什么?

1) User Pages edit with valid information 
    Failure/Error: visit edit_user_path(user) 
    ActionView::Template::Error: 
     wrong number of arguments (0 for 1) 
    # ./app/views/users/edit.html.haml:15:in `block in _app_views_users_edit_html_haml__2466546393595631499_70225077026800' 
    # ./app/views/users/edit.html.haml:6:in `_app_views_users_edit_html_haml__2466546393595631499_70225077026800' 
    # ./spec/requests/user_pages_spec.rb:54:in `block (3 levels) in <top (required)>' 

我不熟悉的阅读Rails的错误信息;这是否告诉我用错误数量的参数调用的方法在edit.html.haml或user_pages_spec.rb中?是否说edit_user_path被0参数调用?

编辑补充我的代码:

edit.html.haml:

- provide(:title, "Edit user") 
%h1 Update your profile 

.row 
    .span6.offset3 
     = form_for(@user) do |f| 

      = f.label :name 
      = f.text_field :name 

      = f.label :email 
      = f.text_field :email 

      = f.label :password 
      = f.password_field 

      = f.label :password_confirmation, "Confirm Password" 
      = f.password_field :password_confirmation 

      = f.submit "Save changes", class: "btn btn-large btn-primary" 

user_pages_spec.rb的相关部分:

describe "edit" do 
     let(:user) { FactoryGirl.create(:user) } 
     before do 
      sign_in user 
      visit edit_user_path(user) 
     end 

     describe "page" do 
      it { should have_selector('h1', text: "Update your profile") } 
      it { should have_selector('title', text: "Edit user") } 
     end 

     describe "with invalid information" do 
      before { click_button "Save changes" } 

      it { should have_content('error') } 
     end 

     describe "with valid information" do 
      let(:new_name) { "New Name" } 
      let(:new_email) { "[email protected]" } 
      before do 
       fill_in "Name",    with: new_name 
       fill_in "Email",   with: new_email 
       fill_in "Password",   with: user.password 
       fill_in "Confirm Password", with: user.password 
       click_button "Save changes" 
      end 

      it { should have_selector('title', text: new_name) } 
      it { should have_success_message('') } 
      it { should have_link('Sign out', href: signout_path) } 
      specify { user.reload.name.should == new_name } 
      specify { user.reload.email.should == new_email } 
     end 
    end 
+2

这将有助于更好地回答,如果你发布你的规范和edit.haml – Ross

+0

@Ross完成,谢谢。 –

回答

8

基本上,参数错误数表示的方法,它认为你应该通过一定数目的变量,它的接收错误的数量。

在这种情况下,它需要1个变量传入并且它没有收到任何数据。

从外观上来看,你应该看你的edit.html.haml文件,我猜的第15行是在这里:

= f.label :password 
= f.password_field 

它看起来像你错过:密码 - 通过实际变量

所以将其更改为:

= f.password_field :password 

,你应该是不错的。

+1

爆炸..... !! – Ross

+0

谢谢!这是否意味着通常,错误块中引用的顶行(例如,_app_views_users_edit_html_haml__2466546393595631499_70225077026800中的'#./app/views/users/edit.html.haml:15:in')指向该问题? –

+3

是的。你正在看的是一个堆栈跟踪或一个支架跟踪。它向您显示了调用方法的顺序。所以ruby正在运行users_pages_spec,并在第54行中遇到了代码,使其进入编辑页面。在第6行的编辑页面上,它遇到了使其转到编辑页面中的另一部分的代码。然后在编辑页面的第15行中遇到错误。 – MrDanA

2

所以堆栈跟踪的顶部是调用最后一个方法,并从那里进行下一步。这表明错误出现在编辑文件中。如果你粘贴了整个编辑文件,它看起来像15缺少密码字段的参数。应该是:

= f.password_field :password 
相关问题