Hartl Ruby-on-Rails Tutorial (3.2), Section 8.2.6 - Exit the link without returning anything

I get the following error after completing the excerpt from the tutorial:

Failures:

1) User pages signup with valid information after saving the user 
 Failure/Error: it { should have_link('Sign out') }
   expected link "Sign out" to return something
 # ./spec/requests/user_pages_spec.rb:49:in `block (5 levels) in <top (required)>'

Finished in 1.15 seconds
48 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:49 # User pages signup with valid information after saving the user 

I used to have more errors, but I ended up reordering the order of the elements in the authentication_pages_spec.rb file. Is the order of things important? What am I missing with this error?

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

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

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

sessions_controller.rb

class SessionsController < ApplicationController

def new
end

def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
  sign_in user
  redirect_to user
else
  flash.now[:error] = 'Invalid email/password combination'
  render 'new'
end
end

def destroy
sign_out
redirect_to root_path
end
end

sessions_helper.rb

module SessionsHelper

def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
current_user = user
end

def current_user=(user)
@current_user = user
end

def current_user
@current_user ||= user_from_remember_token
end



private

def user_from_remember_token
  remember_token = cookies[:remember_token]
  User.find_by_remember_token(remember_token) unless remember_token.nil?
end


def signed_in?
!current_user.nil?
end

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

end

user_pages_spec.rb

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: full_title('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
end

describe "with valid information" do
  before do
    fill_in "Name",         with: "Example User"
    fill_in "Email",        with: "user@example.com"
    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
    it { should have_link('Sign out') }
    end
  end
end

end
+3
source share
1 answer

The operations performed in the "must create user" test are not performed before the "after saving user" test only because after it (ugly explanation).

"" , " ", , (. ).

- , , , ( ):

# user_pages_spec.rb
# ...
describe "with valid information" do
  before do
    fill_in "Name",         with: "Example User"
    fill_in "Email",        with: "user@example.com"
    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 }
    it { should have_link('Sign out') }
  end
end
+3

All Articles