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 User Authentication with Rails Password Hashing and Sign In Integration testing authentication

NoMethodError: undefined method 'authenticate' for nil:NilClass

I'm stuck! In the first test for "Logging In diplays the email address in the event of a failed login", I keep getting a failure with the following error message:

Failure/Error: click_button "Log In" NoMethodError: undefined method `authenticate' for nil:NilClass

app/controllers/user_sessions_controller.rb:13:in 'create' spec/features/users/authentication_spec.rb:19:in 'block (2 levels) in -top (required)-'

Finished in 0.6785 seconds 2 examples, 1 failure

Failed examples:

rspec ./spec/features/users/authentication_spec.rb:15 # Logging In diplays the email address in the event of a failed login

I won't bore you with my code. I've carefully checked it against the zipfile attached to this lesson. I've also downloaded the zipfile , bundled it on my system, run the test on the downloaded folder and I get exactly the same error message!

I'm using an rvm gemset of Ruby 2.1.1 with Rails 4.0.1 ("2.1.1@rails401 on my system). I've had to make a single change to the RSpec configuration to cancel out deprecation warnings. I've added this line to my spec_helper.rb file:

RSpec.configure do |config| config.infer_spec_type_from_file_location! ..... ..... end

Apart from that, and migrating the test and production db's I haven't altered the zipfile at all. What's with this error? Is there a compatibility issue with Ruby 2.1.1 or am I missing something really stupid?

Many thanks for any clues!

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You need to give us more code, ideally - a github repository url to check the project as a whole.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Btw., which version of RSpec do you have? If it's above 2.99, it might be the cause.

Cześć Maciej, thanks for the swift reply!

I'm using the code downloaded from the lesson now, just to make sure. Same error. Looking at my Gemfile.lock though I had:

rspec-core (2.99.2), rspec-expectations (2.99.2), rspec-mocks (2.99.2), rspec-rails (2.99.0) ....etc

So I downloaded again, reset the Gemfile specifically to gem 'rspec-rails', '2.0', re bundled, migrated both db's again. The test is still broken. It now says:

" 'include Capybara' is deprecated. Please use 'include Capybara::DSL' instead." x3

" undefined method 'infer_base_class_for_anonymous_controllers=' for <RSpec::Core::Configuration:0x00000101241510> (NoMethodError)"

It seems to be conflicting with the spec_helper.rb file

Which version of rspec-rails does this work with?

Bardzo dziękuję!

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

It should be working fine with 2.14 and 2.99 as well. Version 3.0 should make some problems. The downloaded project uses version 2.14.7 in the Gemfile.lock.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, I'm getting these failures after downloading the code and running it:

rspec ./spec/features/users/authentication_spec.rb:15 # Logging In diplays the email address in the event of a failed login
rspec ./spec/controllers/user_sessions_controller_spec.rb:32 # UserSessionsController POST 'create' with correct credentials authenticates the user
rspec ./spec/controllers/user_sessions_controller_spec.rb:50 # UserSessionsController POST 'create' with no email in existence behaves like denied login renders the new template
rspec ./spec/controllers/user_sessions_controller_spec.rb:55 # UserSessionsController POST 'create' with no email in existence behaves like denied login sets the flash error message
rspec ./spec/controllers/user_sessions_controller_spec.rb:50 # UserSessionsController POST 'create' with blank credentials behaves like denied login renders the new template
rspec ./spec/controllers/user_sessions_controller_spec.rb:55 # UserSessionsController POST 'create' with blank credentials behaves like denied login sets the flash error message

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Here's my take on this error. This is the authentication_spec file:

require "spec_helper"

describe "Logging In" do
  it "logs the user in and goes to the todo lists" do
    User.create(first_name: "Jason", last_name: "Seifer", email: "jason@teamtreehouse.com", password: "treehouse1", password_confirmation: "treehouse1")
    visit new_user_session_path
    fill_in "Email Address", with: "jason@teamtreehouse.com"
    fill_in "Password", with: "treehouse1"
    click_button "Log In"

    expect(page).to have_content("Todo Lists")
    expect(page).to have_content("Thanks for logging in!")
  end

  it "diplays the email address in the event of a failed login" do
    visit new_user_session_path
    fill_in "Email Address", with: "jason@teamtreehouse.com"
    fill_in "Password", with: "incorrect"
    click_button "Log In"

    expect(page).to have_content("Please check your email and password")
    expect(page).to have_field("Email Address", with: "jason@teamtreehouse.com")
  end
end

As you can see, the new user is created before the first test (and it passes) but the second test does not have any user created. If you add this line:

User.create(first_name: "Jason", last_name: "Seifer", email: "jason@teamtreehouse.com", password: "treehouse1", password_confirmation: "treehouse1")

To the second test, just before the visit, the test will work fine. Which makes sense - it tried to find the user with that email address, got an object that has nil in all attributes and then tried to call authenticate on one of those attributes. So there's nothing wrong with your Ruby or gems, just add this line :)

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

As for the failed controller tests, I didn't look at them long enough yet ;)

Thank you very much!

You're a rockstar Maciej!

That works on my original code as well with rspec 2.99. The controller tests weren't a problem since they only failed with rspec 2.0. Seems that version does't like the "config....... = true/false" syntax in the spec_helper.

Many thanks again!