Search code examples
ruby-on-railsruby-on-rails-3testingrspec-railsrailstutorial.org

Rails Tutorial: Chapter 8.1.5 error (rendering with a flash message; listings 8.11 and 8.12)


I am taking the RoR Tutorial. In the last part of chapter 8.1.5, when all the rspec tests should turn green, I just can't get mine to work. I run bundle exec rspec spec/requests/authentication_pages_spec.rb -e "signin with invalid information" and of the four tests, one fails, with this error:

1) Authentication signin with invalid information after visiting another page 
 Failure/Error: it { should_not have_selector('div.alert.alert-error') }
   expected css "div.alert.alert-error" not to return anything
 # ./spec/requests/authentication_pages_spec.rb:25:in `block (5 levels) in <top (required)>'

The text for app/controllers/sessions_controller.rb is below:

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
      # Sign the user in and redirect to the user's show page.
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

The text for spec/requests/authetication_pages_spec.rb is below:

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

If you have any thoughts or ideas, please let me know, I'm totally stumped!


Solution

  • I cloned your repo and found your problem.

    The links in your header file (app/views/layouts/_header.html.erb) don't go anywhere:

    <li><%= link_to "Home",    '#' %></li>
    <li><%= link_to "Help",    '#' %></li>
    

    So, when your tests are run, the following tests pass because an error is displayed...

    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') }
    

    ...but when you then click on a link that doesn't go anywhere...

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

    ...that error is still on the page, so the test correctly fails.

    Once you change your "Home" link destination from '#' to root_path, your test should pass.

    Looking at the text, if you're on Chapter 8, it seems you missed a step a few chapters back on Listing 5.2.4 in Chapter 5.3.3, so you should be in a better state once you've gone back and fixed it.

    I'd also recommend installing the launchy gem so that you can call helpful Capybara methods like save_and_open_page in your tests and it will open a browser for you with the state of your page at the time you called the method; helps a lot with debugging.