user1446761
user1446761

Reputation: 41

How do I solve rspec errors end of ROR tutorial CH 9

I got to the end of Chapter 9 of Michael Hartl's Ruby on Rails Tutorial and got failed rspec tests. I have gone through the chapter many times and can't find what I did wrong. Please help! Also, I am a two week old programmer so if you could detail the steps you went through debugging that would help me a lot!

My git repo is at https://github.com/kerrieyee/sample_app and The rspec results are as follows:

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 

Upvotes: 1

Views: 1091

Answers (4)

user2785628
user2785628

Reputation:

Alex De Simone is correct I had the same problem and he solved it. Much thanks. Capybara does what we call browser testing. Meaning its kind of like go here click that.

As an example of the problem I had and how I solved it.

the error

Capybara::ElementNotFound

means it was looking for the button to click on and it could not find it. If u look at the code

fill_in "Confirmation", with: "foobar"

then you look at the webpage and it says Confirm Password. You could get your test to pass by typing in

fill_in "Confirm Password", with: "foobar"

you basically just have to match the strings in your capybara test with what it says on the webpage when you look at it.

Upvotes: 0

Alex De Simone
Alex De Simone

Reputation: 592

Make sure the text in the label for :password_confirmation matches in both user_pages_spec.rb and new.html.erb. For instance, you would want the partial in new.html.erb to look like this:

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

and user_pages_spec.rb like this:

    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

Upvotes: 0

sanyorke
sanyorke

Reputation: 51

please note the password_confirmation label text is Confirm Password, you need udpate the spec like below: change fill_in "Confirmation", with: "foobar to fill_in "Confirm Password", with: "foobar I met this issue too, and get solved with above way.

Upvotes: 5

user1446761
user1446761

Reputation: 41

I ended up solving the first 4 failed tests by first taking out the partial I was rendering to in new.html.erb and edit.html.erb. Because I am not an experienced programmer, I am not sure how to fix the code in the tutorial and would appreciate anyone who can tell me what is wrong. This is the refactored code:

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

This is the partial:

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

The last two errors I solved by looking at Michael Hartl's index.html.erb file on github. Essentially, the issue was that the index.html.erb code needed to call the partial _users.html.erb.

Upvotes: 0

Related Questions