3
votes

I was working on Hartl's rails tutorial, end of section 9.2.1. My tests are not passing. Can't figure out why. Hope somebody can be of help. I have attached the error message and the two relevant files 1) authentication_pages_spec.rb and 2) users_controller.rb below

Failures:

1) Authentication authorization for non-signed-in users in the Users controller visiting the edit page Failure/Error: it { should have_title('Sign in') } expected #has_title?("Sign in") to return true, got false # ./spec/requests/authentication_pages_spec.rb:57:in `block (6 levels) in '

2) Authentication authorization for non-signed-in users in the Users controller submitting to the update action Failure/Error: before { patch user_path(user) } ActionController::ParameterMissing: param not found: user # ./app/controllers/users_controller.rb:40:in user_params' # ./app/controllers/users_controller.rb:27:inupdate' # ./spec/requests/authentication_pages_spec.rb:61:in `block (6 levels) in '

Finished in 2.08 seconds 60 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:57 # Authentication authorization for non-signed-in users in the Users controller visiting the edit page rspec ./spec/requests/authentication_pages_spec.rb:62 # Authentication authorization for non-signed-in users in the Users controller submitting to the update action

my authentication_pages_spec.rb is

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', 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 { sign_in user }

      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 "followed 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

users_controller.rb is

 class UsersController < ApplicationController

 def show
@user = User.find(params[:id])
 end

 def new
@user = User.new
 end

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

def edit
    @user = User.find(params[:id])
  end

def update
  @user = User.find(params[:id])
  if @user.update_attributes(user_params)
    flash[:success] = "Profile updated"
    sign_in @user
    redirect_to @user      
  else
    render 'edit'
  end
end


private

  def user_params
    params.require(:user).permit(:name, :email, :password,
                                 :password_confirmation)
  end

  # Before filters

  def signed_in_user
    redirect_to signin_url, notice: "Please sign in." unless signed_in?
  end       
end
1
You should accept an aanswer if it was useful to you...frandroid

1 Answers

4
votes

You are missing a call to your signed_in_user filter on line 2 of your users_controller.rb. You defined the filter at the end of the controller, but you didn't call it! :)

At the very end of the tutorial, that line should look like this:

before_action :signed_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]

However since you have not created followers yet, I think it should look something like this:

before_action :signed_in_user, only: [:index, :edit, :update, :destroy]

I have found it very useful to copy the final source of the code on my hard drive, so I can compare my code to the original whenever I encountered a bug while doing this book.