2012-10-16 27 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 
相關問題