0
votes

I'm working my way through Michael Hartls RoR tutorial which is my first experience with coding.

The last couple of days i've been losing some hair over a rspec failure - hope you guys can help.

Mhartl, chap 9 - Listing 9.3

bundle exec rspec spec/requests/user_pages_spec.rb -e "edit page" Run options: include {:full_description=>/edit\ page/}

    Failure:
    1) User pages edit page 
         Failure/Error: it { should have_selector('title', text: "Edit user") }
           expected css "title" with text "Edit user" to return something
         # ./spec/requests/user_pages_spec.rb:75:in `block (4 levels) in <top (required)>'

3 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:75 # User pages edit page 

User pages spec:

 require 'spec_helper'

    describe "User pages" do

      subject { page }

      describe "signup page" do
        before { visit signup_path }

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

      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" 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') }
            it { should_not have_content('Password digest') }
          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') }
            it { should have_link('Sign out') }
          end
        end
      end

      describe "edit" do
        let(:user) { FactoryGirl.create(:user) }
        before { visit edit_user_path(user) }


        describe "page" do

          it { should have_selector('h1',    text: "Update your profile") }
          it { should have_selector('title', text: "Edit user") }
          it { should have_link('change', href: 'http://gravatar.com/emails') }
        end

        describe "with invalid information" do
          before { click_button "Save changes" }

          it { should have_content('error') }
        end
      end
    end

authentication pages spec

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

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

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user }

      it { should have_selector('title', text: 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
end

Users Controller

class UsersController < ApplicationController

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

  def new
    @user = User.new
  end

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

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

Sessions helper

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

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end
end

Utilities

include ApplicationHelper

def valid_signin(user)
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
end

RSpec::Matchers.define :have_error_message do |message|
  match do |page|
    page.should have_selector('div.alert.alert-error', text: message)
  end
end
4

4 Answers

1
votes

The failure disappeared after restarting the server!

0
votes

Are you sure the page it's querying actually has a title? It looks like it can't find the title in the HTML. Check your view for that page, you should be setting the title somewhere. The Rspec failure looks like it can't find title in the DOM and is returning nil instead.

0
votes

Try adding:

<% provide(:title, "Edit user") %>

to the top of your app/views/users/edit.html.erb file.

0
votes

Also, make sure you restart Spork, if you are using it for testing. I ran into the same issue.