2012-06-23 65 views
1

我到了Michael Hartl的Ruby on Rails教程第9章的末尾,並且rspec測試失敗。我已經多次閱讀了這一章,並且找不到我做錯了什麼。請幫忙!另外,我是一個兩週的程序員,所以如果你能詳細說明你通過調試的步驟,那將會對我有很大幫助!我如何解決rspec錯誤ROR教程結束CH 9

我的混帳回購協議是在https://github.com/kerrieyee/sample_app和RSpec的結果如下:

Macintosh-143:sample_app jeffreyyee$ bundle exec rspec spec/ 
..................................................................FF........FFFF......... 

Failures: 

    1) User pages index delete links as an admin user 
Failure/Error: it { should have_link('delete', href: user_path(User.first)) } 
    expected link "delete" to return something 
# ./spec/requests/user_pages_spec.rb:45:in `block (5 levels) in <top (required)>' 

    2) User pages index delete links as an admin user should be able to delete another user 
Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1) 
Capybara::ElementNotFound: 
    no link with title, id or text 'delete' found 
# (eval):2:in `click_link' 
# ./spec/requests/user_pages_spec.rb:47:in `block (6 levels) in <top (required)>' 
# ./spec/requests/user_pages_spec.rb:47:in `block (5 levels) in <top (required)>' 

    3) User pages signup with valid information should create a user 
Failure/Error: fill_in "Confirmation", with: "foobar" 
Capybara::ElementNotFound: 
    cannot fill in, no text field, text area or password field with id, name, or label 'Confirmation' found 
# (eval):2:in `fill_in' 
# ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>' 

    4) User pages signup with valid information after saving the user 
Failure/Error: fill_in "Confirmation", with: "foobar" 
Capybara::ElementNotFound: 
    cannot fill in, no text field, text area or password field with id, name, or label 'Confirmation' found 
# (eval):2:in `fill_in' 
# ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>' 

    5) User pages signup with valid information after saving the user 
Failure/Error: fill_in "Confirmation", with: "foobar" 
Capybara::ElementNotFound: 
    cannot fill in, no text field, text area or password field with id, name, or label 'Confirmation' found 
# (eval):2:in `fill_in' 
# ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>' 

    6) User pages signup with valid information after saving the user 
Failure/Error: fill_in "Confirmation", with: "foobar" 
Capybara::ElementNotFound: 
    cannot fill in, no text field, text area or password field with id, name, or label 'Confirmation' found 
# (eval):2:in `fill_in' 
# ./spec/requests/user_pages_spec.rb:96:in `block (4 levels) in <top (required)>' 

Finished in 4.83 seconds 
89 examples, 6 failures 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:45 # User pages index delete links as an admin user 
rspec ./spec/requests/user_pages_spec.rb:46 # User pages index delete links as an admin user should be able to delete another user 
rspec ./spec/requests/user_pages_spec.rb:99 # User pages signup with valid information should create a user 
rspec ./spec/requests/user_pages_spec.rb:108 # User pages signup with valid information after saving the user 
rspec ./spec/requests/user_pages_spec.rb:109 # User pages signup with valid information after saving the user 
rspec ./spec/requests/user_pages_spec.rb:110 # User pages signup with valid information after saving the user 

回答

0

我最終解決前4先取出部分我被渲染到new.html.erb測試失敗和edit.html.erb。因爲我不是一個有經驗的程序員,所以我不確定如何修復本教程中的代碼,並希望任何能告訴我哪裏出錯的人。這是重構的代碼:

<% provide(:title, 'Sign up') %> 
<h1>Sign up</h1> 

<div class="row"> 
    <div class="span6 offset3"> 
    <%= form_for(@user) do |f| %> 
     <%= render 'fields', f: f %> 
     <%= f.submit "Create my account", class: "btn btn-large btn-primary" %> 
    <% end %> 
    </div> 
</div> 

這是部分:

<%= render 'shared/error_messages' %> 

<%= f.label :name %> 
<%= f.text_field :name %> 

<%= f.label :email %> 
<%= f.text_field :email %> 

<%= f.label :password %> 
<%= f.password_field :password %> 

<%= f.label :password_confirmation, "Confirm Password" %> 
<%= f.password_field :password_confirmation %> 

最後兩個錯誤,我通過在GitHub上看着Michael Hartl's index.html.erb file解決。本質上,問題是index.html.erb代碼需要調用partial _users.html.erb。

5

請注意password_confirmation標籤文字確認密碼,則需要UDPATE規範象下面這樣: 變化 FILL_IN 「確認書」,以 「foobar的 到 FILL_IN 」確認密碼「,用」 foobar的 我也遇到過這個問題,並以上述方式解決。

1

確保password_confirmation標籤中的文本與user_pages_spec.rb和new.html.erb中的文本匹配。例如,你想分在new.html.erb看起來像這樣:

<%= render 'shared/error_messages' %> 
     <%= f.label :name %> 
     <%= f.text_field :name %> 
     <%= f.label :email %> 
     <%= f.text_field :email %> 
     <%= f.label :password %> 
     <%= f.password_field :password %> 
     <%= f.label :password_confirmation, "Confirm Password" %> 
     <%= f.password_field :password_confirmation %> 
     <%= f.submit "Create my account", class: "btn btn-large btn-primary" %> 
    <% end %> 

和user_pages_spec.rb這樣的:

describe "with valid information" do 
     before do 
      fill_in "Name",    with: "Example User" 
      fill_in "Email",   with: "[email protected]" 
      fill_in "Password",   with: "foobar" 
      fill_in "Confirm Password", with: "foobar" 
     end 
     it "should create a user" do 
      expect { click_button submit}.to change(User, :count).by(1) 
     end 
    end 
0

亞歷克斯·西蒙娜是正確的我有同樣的問題他解決了它。非常感謝。水豚做我們稱之爲瀏覽器測試。這意味着它有點像去這裏點擊。

作爲我遇到的問題的一個例子,以及我如何解決它。

錯誤

Capybara::ElementNotFound 

意味着它正在尋找按鈕,點擊它無法找到它。如果你看代碼

fill_in "Confirmation", with: "foobar" 

然後你看看網頁,它說確認密碼。你可以讓你的測試通過鍵入

fill_in "Confirm Password", with: "foobar" 

傳給你基本上只需要在你的水豚測試琴絃用什麼,當你看着它,它說在網頁上匹配。

相關問題