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

Nelly Nelly
Nelly Nelly
7,134 Points

Failure/Error: expect(page).to have_content..... Keep failing.....

Hello everybody...

I am stuck here, what am I doing wrong?

my terminal

Logging In
  logs the user and goes to the todo list
  displays the email address in the event of the failed login (FAILED - 1)

Failures:

  1) Logging In displays the email address in the event of the failed login
     Failure/Error: expect(page).to have_content("Please check your email and password!")
       expected to find text "Please check your email and password!" in "Odot Todo Lists Sign Up Log In Email Address Password"
     # ./spec/features/users/authentication_spec.rb:23:in `block (2 levels) in <top (required)>'

Finished in 0.50424 seconds (files took 1.45 seconds to load)
2 examples, 1 failure

Failed examples:

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

authentication_spec.rb

require 'rails_helper'
require 'rspec/active_model/mocks'

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

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

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

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

  end
end

user_sessions_controller.rb

class UserSessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:email])
    if user && user.authenticate(params[:password])
      session[:user_id] = user.id
      flash[:success]= "Thanks for logging in!"
      redirect_to todo_lists_path
    else
      render :new
      flash[:error]= "Please check your email and password!"
    end
  end
end

Thanks for your help

1 Answer

HI Nelly,

I think your test is fine; it is failing for good reason without syntax error.

The issue may be, therefore, in your controller code. I would try switching your two lines of code in the else clause. Try flashing the error first, then rendering new. And, I'd be tempted to put a space after the closing square bracket, before the equls sign. I don't think that causes a syntax issue but I've found spacing issues like that can have unexpected consequences at times!

Let me know how you get on.

Steve.

Nelly Nelly
Nelly Nelly
7,134 Points

OH MY !! That was it ! Many thanks !

Whoop! \o/ :+1:

Nelly Nelly
Nelly Nelly
7,134 Points

only switched the two lines you mentionned :D