Neil89
Neil89

Reputation: 111

Michael Hartl's Ruby on Rails tutorial failed test Chapter 8

I'm new on Rails. I'm following the tutorial of Michael Hartl, Ruby on Rails Tutorial - Learn Web Development with Rails (http://ruby.railstutorial.org/). I've found an error when I try to pass some tests. My output when I execute bundle exec rspec is the next:

.........................................F.....

Failures:

  1) Authentication signin with invalid information 
     Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
       expected css "div.alert.alert-error" with text "Invalid" to return something
     # ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'

Finished in 1.39 seconds
47 examples, 1 failure

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:23 # Authentication signin with invalid information

The next files have been more highly changed and I think one of them may be causing the error:

authentication_pages_rspec.rb

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end


describe "signin" do

    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_selector('title', text: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }

      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before do
        fill_in "Email",    with: user.email
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      it { should have_selector('title', text: user.name) }
      it { should have_link('Profile', href: user_path(user)) }
      it { should have_link('Sign out', href: signout_path) }
      it { should_not have_link('Sign in', href: signin_path) }
    end
  end
end

sessions_controler.rb

class SessionsController `>` ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

sessions_helper.rb

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user;
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end
end

If you need any other file, please let me know and I'll post it. Thank you in advance.

Upvotes: 2

Views: 1008

Answers (1)

bento
bento

Reputation: 2099

I've browsed through your code, and apart from the semicolon in

 self.current_user = user;

I haven't seen anything strange. You haven't posted the method you're testing,

if user && user.authenticate(params[:session][:password])

though, so the problem might be there.

General steps to find the issue:

  • restart spark if you're using it.
  • test the failing spec manually: does the functionality work?
  • if not, fix it
  • if the functionality should work, but the spec still fails, use launchy and save_and_open_page to peek at the page during your test.

Addition 1:

well, that part in your spec

before { visit signin_path }

describe "with invalid information" do
  before { click_button "Sign in" }

leads you to the SessionController create action (because the link links to signin_path which is mapped there in config/routes.rb) and the main logic that could go wrong is user.authenticate(params[:session][:password]) - which is provided by has_secure_password.

Upvotes: 2

Related Questions