1

Michael Hartl の Ruby on Rails Tutorial の第 9 章まで到達し、rspec テストに失敗しました。この章を何度も読みましたが、どこが間違っていたのかわかりません。助けてください!また、私は生後 2 週間のプログラマーなので、デバッグの手順を詳しく教えていただければ、とても助かります。

私の git リポジトリは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 
4

4 に答える 4

5

password_confirmation ラベル テキストはConfirm Passwordであることに注意してください。仕様を次のように更新する必要があります: change fill_in "Confirmation", with: "foobar to fill_in "Confirm Password", with: "foobar I met this issue too, and get resolve with above仕方。

于 2012-07-31T14:04:46.557 に答える
0

new.html.erb と edit.html.erb でレンダリングしていたパーシャルを最初に取り出して、最初の 4 つの失敗したテストを解決しました。私は経験豊富なプログラマーではないため、チュートリアルのコードを修正する方法がわかりません。どこが間違っているのか教えていただけると助かります。これはリファクタリングされたコードです:

<% 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 %>

最後の 2 つのエラーは、githubの Michael Hartl の index.html.erb ファイルを見て解決しました。基本的に、問題は index.html.erb コードが部分的な _users.html.erb を呼び出す必要があることでした。

于 2012-06-24T01:12:23.323 に答える
0

: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: "user@example.com"
            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
于 2014-03-12T19:28:17.197 に答える