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 Resets and Testing Integration Testing Forgotten Passwords

sungwon choe
sungwon choe
15,044 Points

NoMethodError: undefined method `click_link' for nil:NilClass

it "resets a password when following the email link" continues to fail for me

 1) Forgotten passwords resets a password when following the email link
     Failure/Error: current_email.click_link "http://"
     NoMethodError:
       undefined method `click_link' for nil:NilClass
     # ./spec/features/users/forgot_password_spec.rb:21:in `block (2 levels) in <top (required)>'

I have added the capybara-email gem to the Gemfile (gem 'capybara-email', '~> 2.2.0') and "bundle"d it and added "require 'capybara/email/rspec'" to my spec_helper.rb file.

Googling for this error turned up a conflict with the email_spec gem, but this gem is not used in this project.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Can you post your whole spec and the view that it relates to?

sungwon choe
sungwon choe
15,044 Points

Thanks for the reply. Sure:

forgot_password_spec.rb

require 'spec_helper'

describe "Forgotten passwords" do
    let(:user) { create(:user)}

    it "sends a user an email" do
        visit login_path
        click_link "Forgot Password"
        fill_in "Email", with: user.email
        expect {
            click_button "Reset Password"
        }.to change{ ActionMailer::Base.deliveries.size}.by(1)
    end

    it "resets a password when following the email link" do
        visit login_path
        click_link "Forgot Password"
        fill_in "Email", with: user.email
        click_button "Reset Password"
        open_email(user.email)
        current_email.click_link "http://"
        expect(page).to have_content("Change Your Password")

    end

end

password_reset.text.html.erb

<p>Hi <%= @user.first_name %>,</p>

<p>You can reset your password here:</p>

<p><%= link_to edit_password_reset_url(@user.password_reset_token), 
   <%= edit_password_reset_url(@user.password_reset_token) %></p>

password_reset_text.erb

Hi <%= @user.first_name %>,

You can reset your password here:

<%= edit_password_reset_url(@user.password_reset_token) %>

and my Gemfile

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

gem 'minitest'

group :development, :test do
    gem 'rspec-rails', '~> 2.0'
    gem 'factory_girl_rails', '~> 4.0'
end

group :test do
    gem 'capybara', '~>2.1.0'
    gem 'capybara-email', '~> 2.2.0'
    gem 'shoulda-matchers', '~>2.4.0'
end

gem 'bcrypt-ruby', '~>3.1.2'

Thanks for any suggestions you may have!

Luke Wenke
Luke Wenke
32,294 Points

I get the same problem. There should be a solution without having to download his code from the video.

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, your current_email does not have any value for some reason (so it's nil), the test is trying to use the click_link method on it and it fails because click_link does not work on nil values. Go back to where the current_email is defined and where it gets assigned a value and see what could have gone wrong there.

sungwon choe
sungwon choe
15,044 Points

Yep.

current_email is given with the capybara-email gem somehow. I think this is some sort of configuration problem as the only major difference I can find between my code and the code given in the download link is that I'm using a different version of Rails.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This could be a cause, although I have no way of knowing. Try downloading code from that video, bundle install, migrate the database and try running tests for it to see if the same failure occurs.

sungwon choe
sungwon choe
15,044 Points

Yes, I did. The tests for the video code worked without error. Thanks for your comments!

Jaime Young
Jaime Young
8,110 Points

same but everything worked fine. I think the problem is in the difference of versions / configurations.

:D

Kevin Mulhern
Kevin Mulhern
20,374 Points

Having the same problem :(. Anybody have a solution?