0

I am working on Michael Hartl's Rails Tutorial, Chapter 8.2.4: http://www.railstutorial.org/book/sign_in_out#sec-changing_the_layout_links

So far, everything seems to have worked great.

However, when I run the tests at the end of the section, the tutorial says they should pass, but I get the following errors:

FFFF.....*....................................

Pending:
  SessionsHelper add some examples to (or delete) /Users/Thibaud/work/rails_projects/sample_app/spec/helpers/sessions_helper_spec.rb
    # No reason given
    # ./spec/helpers/sessions_helper_spec.rb:14

Failures:

  1) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Thibaud/work/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

  2) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Thibaud/work/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

  3) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Thibaud/work/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

  4) Authentication signin with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Thibaud/work/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

Finished in 2.04 seconds
46 examples, 4 failures, 1 pending

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:40 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:38 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:39 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:37 # Authentication signin with valid information 

Randomized with seed 59639

Here is what's in 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('Sign out',    href: signout_path) }
            it { should_not have_link('Sign in', href: signin_path) }
        end
    end
end

[EDIT:] Here is the SessionController file:

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    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
    sign_out
    redirect_to root_url
  end
end

How can I make the tests to pass?

Thanks.

Thibaud Clement
  • 6,607
  • 10
  • 50
  • 103

1 Answers1

0

Without seeing the code for your SessionController class it's a little difficult to tell exactly where you are going wrong. At the end of the tutorial, the create action/method in your SessionController should look like this:

def create
  @user = User.new(user_params)
  if @user.save
    sign_in @user
    flash[:success] = "Welcome to the Sample App!"
    redirect_to @user
  else
    render 'new'
  end
end

In this code if @user saves appropriately, the redirect_to @user portion of the code tells the controller to redirect to @user, or makes a GET request to the user path which, according to the RESTful routes, will return the user/show view. If that fails to save, the controller render's the new view (app/views/sessions/new.html.erb).

Without seeing your controller, I'd suspect you are failing to redirect the controller to another action or render another view (since it's yelling about missing a template for create).

Here's another answer on SO indicating the same.

Community
  • 1
  • 1
  • Or you can make it ok to render no view. See [here](http://stackoverflow.com/a/10341470/792916). – Elena G. Washington Jun 29 '14 at 01:46
  • Thanks a lot Elena. I just completed my question by adding the content of the SessionController file. There are some slight differences with the code you mentioned, maybe because I am not at the end of the tutorial yet. What do you think? – Thibaud Clement Jun 29 '14 at 16:40
  • Ok, it looks like there isn't anything in the first part of your if block. There's only a comment which doesn't execute any code. In your code, if the user exists and is authenticated, the controller thinks it should pass the "create" view (because that's the method/action it's using and you didn't tell it to do anything else). But that's not what you want. Later on in the tutorial you add code to redirect the user and bypass this error. The rspec tests are failing appropriately for now. Finish the tutorial and they should pass. :) – Elena G. Washington Jun 29 '14 at 17:23
  • Perfect, thank you very much. I was just afraid that the fact the tests are not passing would become a bigger problem later on, but I am going to keep moving and see how it turns. – Thibaud Clement Jun 29 '14 at 20:02