Erv Noel
Erv Noel

Reputation: 85

Ruby Rails Tutorial 8.25

Pretty new to Rails and I can't really figure out why my tests won't pass right before I get to 8.25 in Hartl's Rails Tutorial. Here are the errors, and some code. I think it's maybe something trivial that I may be overlooking but I've looked through it several times and I still cannot figure it out. Any help is greatly appreciated ! Also, please let me know if I should add more code

1) Authentication signin with valid information 
 Failure/Error: it { should have_link('Settings', href: edit_user_path(user)) }
   expected link "Settings" to return something
 # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <top (required)>'

  2) Authentication signin with valid information 
 Failure/Error: it { should have_link('Users',    href: users_path) }
   expected link "Users" to return something
 # ./spec/requests/authentication_pages_spec.rb:39:in `block (4 levels) in <top (required)>'

Finished in 2.38 seconds
56 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:41 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:39 # Authentication signin with valid information 

user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_selector('h1', text: user.name) }
    it { should have_selector('title', text: user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

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


    describe "signup" do

      before { visit signup_path }

      let(:submit) { "Create my account" }


      describe "with invalid information" do
        it "should not create a user" do
          expect { click_button submit }.not_to change(User, :count)
        end

        describe "after submission" do
          before { click_button submit }

          it { should have_selector('title', text: 'Sign up') }
          it { should have_content('error') }
        end
      end

        describe "with valid information" do
          before do
            fill_in "Name",         with: "Example User"
            fill_in "Email",        with: "[email protected]"
            fill_in "Password",     with: "foobar"
            fill_in "Confirmation", with: "foobar"
          end

        it "should create a user" do
            expect { click_button submit }.to change(User, :count).by(1)
        end

        describe "after saving the user" do
          before { click_button submit }

          let(:user) { User.find_by_email('[email protected]') }

          it { should have_selector('title', text: user.name) }
          it { should have_selector('div.alert.alert-success', text: 'Welcome') }
          end  
        end
      end
    end

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

      it { should have_selector('title', text: user.name) }

      it { should have_link('Users',    href: users_path) }
      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) }
      end
    end
  end

app/helpers/session_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

Upvotes: 0

Views: 149

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

In SessionsHelper#current_user you have line:

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

Since cookies are the method that returns cookies Hash (HashWithIndifferentAccess, to be precise), it should be:

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

Upvotes: 2

Related Questions