Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

Rspec / Capybara testing fail and not sure why

Hey guys im trying to test the input to a table that was created with generate scaffold command. it has been migrated ect and gemfile updated, however when i write the test to enter the First_Name and Last_Name (see code below for test code) i get the following error:

TEST CODE:

require 'spec_helper'

describe "Create User lists" do
    it "redirects to the users list index page on success" do
        visit "/users"
        click_link "New User"
        expect(page).to have_content("New User")

        fill_in "First_Name", with: "Shawn"
        fill_in "Last_Name", with: "Wilson"
        click_button "Create User"

        expect(page).to have_content("Shawn")
        end
    end

command line error after test is run:

2 deprecation warnings total

Finished in 0.0911 seconds 1 example, 1 failure

Failed examples:

rspec ./spec/features/users/create_spec.rb:4 # Create User lists redirects to the users list index page on success

Randomized with seed 10541

there are a bunch more feilds that i will test but they are not validated or required at this time. any and all help is appreciated!

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Here's the explanation of your error:

Unable to find field "First_Name"

If it's all happening in /users, then go to localhost:3000/users, click New User (like the test does) and see for yourself how the fields are named and use these names in your capybara tests. The appropriate view file should be something like app/views/users/new.html.erb.

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

so when i look at the field on the form itself in the localhost:3000/users page the fields are not separated by _ is this the issue? when i look at other pages in the text editor (edit.html.erb_spec.rb) they all have the _ included. so by removing this it should work? sorry i'm not sure why i'm so slow at this today..

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You have to distinguish between what is seen in the browser by Capybara and the internal names of the fields used in the database and controllers. In Capybara you use the names that you see in the browser. These names can be defined in the views, but if they are not defined, their default names will be generated by Rails based on the internal attribute names. Rails has its own template for this. Here's an example of a new.html.erb form field:

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

The above code generates default label for the field. It will be something like "Password confirmation". And that's what I'd have to use in Capybara tests. But I can override this default and have my own name for the label:

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

And now I'd have to use "Confirm Password" in my tests.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This is not the full error message. You should have more info above what you quoted - it contains what the test expected AND what it got. This would tell you why this failed. Please run the server and manually try doing these steps. Make sure that the links and fields are really named the way you did in tests. For example is there really a label named "First_Name", or maybe it's "First Name" or "First name"? Underscores and upper/lower case matter.

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

So this is what the command prompt feeds back to me after hitting enter:

Shawns-MacBook-Pro:SafeReporAPP TaurenLTD1$ bin/rspec spec/features/users/create_spec.rb F

Failures:

1) Create User lists redirects to the users list index page on success Failure/Error: fill_in "First_Name", with: "Shawn" Capybara::ElementNotFound: Unable to find field "First_Name" # ./spec/features/users/create_spec.rb:9:in `block (2 levels) in '

Deprecation Warnings:

RSpec::Core::ExampleGroup#example is deprecated and will be removed in RSpec 3. There are a few options for what you can use instead:

rspec-core's DSL methods (it, before, after, let, subject, etc) now yield the example as a block argument, and that is the recommended way to access the current example from those contexts. The current example is now exposed via RSpec.current_example, which is accessible from any context. If you can't update the code at this call site (e.g. because it is in an extension gem), you can use this snippet to continue making this method available in RSpec 2.99 and RSpec 3:

RSpec.configure do |c| c.expose_current_running_example_as :example end

(Called from /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/capybara-2.1.0/lib/capybara/rspec.rb:20:in `block (2 levels) in ')

RSpec::Core::ExampleGroup#example is deprecated and will be removed in RSpec 3. There are a few options for what you can use instead:

rspec-core's DSL methods (it, before, after, let, subject, etc) now yield the example as a block argument, and that is the recommended way to access the current example from those contexts. The current example is now exposed via RSpec.current_example, which is accessible from any context. If you can't update the code at this call site (e.g. because it is in an extension gem), you can use this snippet to continue making this method available in RSpec 2.99 and RSpec 3:

RSpec.configure do |c| c.expose_current_running_example_as :example end

(Called from /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/capybara-2.1.0/lib/capybara/rspec.rb:21:in `block (2 levels) in ')

If you need more of the backtrace for any of these deprecations to identify where to make the necessary changes, you can configure config.raise_errors_for_deprecations!, and it will turn the deprecation warnings into errors, giving you the full backtrace.

2 deprecation warnings total

Finished in 0.37773 seconds 1 example, 1 failure

Failed examples:

rspec ./spec/features/users/create_spec.rb:4 # Create User lists redirects to the users list index page on success

Randomized with seed 29251

my big issue is that i still really dont understand where to go to even check the input names im jumping around in the ruby project looking but not too surr where to go to look up the values. i know when i created the input names in the scaffold generator i used Word_One Word_Two ect for any name with 2 words in it.

Shawn Wilson
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Wilson
iOS Development Techdegree Student 7,049 Points

Thanks Maciej Czuchnowski, you gave me the Ah-ha moment i was looking for here! thanks for all your help.. im sure we will cross paths down the road.. lol..