0
votes

I am taking the RoR Tutorial. When all the rspec tests should turn green, I just can't get mine to work. I run bundle exec rspec spec/ and of the four tests, one fails, with this error:

Failures:

1) Authentication signin with invalid information after visiting another page

 Failure/Error: it { should_not have_selector('div.alert.alert-error') }
   expected #has_selector?("div.alert.alert-error") to return false, got true
 # ./spec/requests/authentication_pages_spec.rb:25:in `block (5 levels) in <top (required)>'Finished in 7.2 seconds 62 examples, 1

failure

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:25 # Authentication signin wi th invalid information after visiting another page

My authentication_pages_spec.rb file:

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_content('Sign in') }
    it { should have_title('Sign in') }
  end

describe "signin" do
    before { visit signin_path }

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

      it { should have_title('Sign in') }
      it { should have_selector('div.alert.alert-error') }

    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.upcase
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      it { should have_title(user.name) }
      it { should have_link('Profile',     href: user_path(user)) }
      it { should have_link('Settings',    href: edit_user_path(user)) }
      it { should have_link('Sign out',    href: signout_path) }
      it { should_not have_link('Sign in', href: signin_path) }



    describe "follower by signout" do
      before {click_link "Sign out"}
      it {should have_link('Sign in')}
      end
    end
  end

 describe "authorization" do

    describe "for non-signed-in users" do
      let(:user) { FactoryGirl.create(:user) }

      describe "in the Users controller" do

        describe "visiting the edit page" do
          before { visit edit_user_path(user) }
          it { should have_title('Sign in') }
        end

        describe "submitting to the update action" do
          before { patch user_path(user) }
          specify { expect(response).to redirect_to(signin_path) }
        end
      end
    end
  end
 end

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag "application", media: "all",
                                           "data-turbolinks-track" => true %>
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <% flash.each do |key, value| %>
        <%= content_tag(:div, value, class: "alert alert-#{key}") %>
      <% end %>
      <%= yield %>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html>

home.html.erb :

<div class="center hero-unit">
  <h1>Welcome to the Sample App</h1>

  <h2>
    This is the home page for the
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
  </h2>

  <%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>

static_pages_controller.rb:

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  end

  def contact
  end
end
1
Can you share the Home page in the question?Kirti Thorat♦
@detskai can you show your layout code i.e. app/views/layouts/application.html.erb ?Sachin Singh
@detskai Can you also share the Home page.Kirti Thorat♦
@detskai if your root_path is root 'static_pages#home', then please show your root static_pages controller.Sachin Singh
@SachinSingh yes, root 'static_pages#home' , static_pages_controller addeddetskai

1 Answers

1
votes

Please make sure your sessions_controller.rb has the create action defined like this:

def create      
    user = User.find_by(email: params[:email].downcase)
    if user && user.authenticate(params[:password])
        # If the user exists and they give a correct password,
        # sign the user in and redirect to the user's show page.
        sign_in user
        redirect_to user 
    else
        # Create an error message and re-render the signin form.
        flash.now[:error] = 'Invalid email/password combination'
        render 'new'
    end
end

Pay attention to the flash.now. It's not the same as just flash that we have used in other sections of the tutorial.