0

I am taking the RoR Tutorial(chapter 9.4). When all the rspec tests should turn green, I just can't get mine to work. I run bundle exec rspec spec/ and get four fails, with this error:

Failures:

  1) User pages index signup page with valid information after saving the user
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `email' for nil:NilClass
     # ./spec/support/utilities.rb:23:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (requi
red)>'

  2) User pages index signup page with valid information after saving the user
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `email' for nil:NilClass
     # ./spec/support/utilities.rb:23:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (requi
red)>'

  3) User pages index signup page with valid information after saving the user
     Failure/Error: sign_in user
     NoMethodError:
       undefined method `email' for nil:NilClass
     # ./spec/support/utilities.rb:23:in `sign_in'
     # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (requi
red)>'

  4) Authentication signin with invalid information after visiting another page

     Failure/Error: it { should_not have_selector('div.alert.alert-error') }
       expected #has_selector?("div.alert.alert-error") to return false, got tru
e
     # ./spec/requests/authentication_pages_spec.rb:26:in `block (5 levels) in <
top (required)>'

Finished in 21.33 seconds
81 examples, 4 failures

utilities.rb:

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|
    expect(page).to have_selector('div.alert.alert-error', text: message)
  end
end

def sign_in(user, options={})
  if options[:no_capybara]
    # Sign in when not using Capybara.
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
  else
    visit signin_path
    fill_in "Email",    with: user.email
    fill_in "Password", with: user.password
    click_button "Sign in"
  end
end

user_page_spec.rb:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "index" do

    let(:user) { FactoryGirl.create(:user) }

    before do
      sign_in user
      visit users_path
    end

    it { should have_title('All users') }
    it { should have_content('All users') }

    describe "pagination" do

      before(:all) { 30.times { FactoryGirl.create(:user) } }
      after(:all)  { User.delete_all }

      it { should have_selector('div.pagination') }

      it "should list each user" do
        User.paginate(page: 1).each do |user|
          expect(page).to have_selector('li', text: user.name)
        end
      end
    end

   describe "delete links" do

      it { should_not have_link('delete') }

      describe "delete links" do

      it { should_not have_link('delete') }

      describe "as an admin user" do
        let(:admin) { FactoryGirl.create(:admin) }
        before do
          sign_in admin
          visit users_path
        end

        it { should have_link('delete', href: user_path(User.first)) }
        it "should be able to delete another user" do
          expect do
            click_link('delete', match: :first)
          end.to change(User, :count).by(-1)
        end
        it { should_not have_link('delete', href: user_path(admin)) }
      end
    end
  end


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

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

    describe "signup page" 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: "[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_link('Sign out') }
        it { should have_title(user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }
      end

    end
  end

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

    describe "page" do
      it { should have_content("Update your profile") }
      it { should have_title("Edit user") }
      it { should have_link('change', href: 'http://gravatar.com/emails') }
    end

    describe "with valid information" do
      let(:new_name)  { "New Name" }
      let(:new_email) { "[email protected]" }
      before do
        fill_in "Name",             with: new_name
        fill_in "Email",            with: new_email
        fill_in "Password",         with: user.password
        fill_in "Confirm Password", with: user.password
        click_button "Save changes"
      end

      it { should have_title(new_name) }
      it { should have_selector('div.alert.alert-success') }
      it { should have_link('Sign out', href: signout_path) }
      specify { expect(user.reload.name).to  eq new_name }
      specify { expect(user.reload.email).to eq new_email }
    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_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 { sign_in user }

      it { should have_title(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) }



    describe "follower 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 "when attempting to visit a protected page" do
        before do
          visit edit_user_path(user)
          fill_in "Email", with: user.email
          fill_in "Password", with: user.password
          click_button "Sign in"
        end

        describe "after signing in" do

          it "should render the desired protected page" do
            expect(page).to have_title('Edit user')
          end
        end
      end

      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
        describe "visiting the user index" do
          before { visit users_path }
          it { should have_title('Sign in') }
        end

    end

    describe "as wrong user" do
      let(:user) { FactoryGirl.create(:user) }
      let(:wrong_user) { FactoryGirl.create(:user, email: "[email protected]") }
      before { sign_in user, no_capybara: true }

      describe "submitting a GET request to the Users#edit action" do
        before { get edit_user_path(wrong_user) }
        specify { expect(response.body).not_to match(full_title('Edit user')) }
        specify { expect(response).to redirect_to(root_url) }
      end

      describe "submitting a PATCH request to the Users#update action" do
        before { patch user_path(wrong_user) }
        specify { expect(response).to redirect_to(root_url) }
      end
    end

   describe "as non-admin user" do
      let(:user) { FactoryGirl.create(:user) }
      let(:non_admin) { FactoryGirl.create(:user) }

      before { sign_in non_admin, no_capybara: true }

      describe "submitting a DELETE request to the Users#destroy action" do
        before { delete user_path(user) }
        specify { expect(response).to redirect_to(root_url) }
      end
    end
  end
end

user.rb

class User < ActiveRecord::Base
  has_secure_password
  before_save { self.email = email.downcase }
  before_create :create_remember_token
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private

    def create_remember_token
      self.remember_token = User.encrypt(User.new_remember_token)
    end

end

factories.rb:

FactoryGirl.define do
  factory :user do
    sequence(:name)  { |n| "Person #{n}" }
    sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar"
    password_confirmation "foobar"

    factory :admin do
      admin true
    end
  end
end

rails console test:

irb(main):001:0> **user = FactoryGirl.build(:user)**
=> #<User id: nil, name: "Person 1", email: "[email protected]", created_at:
nil, updated_at: nil, password_digest: "$2a$04$LiRdaarU6QaX9PJa1uFjE.5e44SYRsmy1
OMY8A4EKZow...", remember_token: nil, admin: false>

irb(main):002:0> **user.valid?**
  User Exists (1.0ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email")
 = LOWER('[email protected]') LIMIT 1
=> true

irb(main):003:0> **user.errors**
=> #<ActiveModel::Errors:0x49dd678 @base=#<User id: nil, name: "Person 1", email
: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2
a$04$LiRdaarU6QaX9PJa1uFjE.5e44SYRsmy1OMY8A4EKZow...", remember_token: nil, admi
n: false>, @messages={}>

I changed users_controller.rb and now i have just 3 (1-3) failures

users_controller.rb:

          class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    @users = User.paginate(page: params[:page])
  end

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

  def new
    @user = User.new
  end

   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

   def edit
   end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user 
      # Handle a successful update.
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted."
    redirect_to users_url
  end

  private

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

    # Before filters

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end


    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end


  def admin_user
      redirect_to(root_url) unless current_user.admin?
    end

end

Actual project version - https://tranquil-waters-6116.herokuapp.com . Maybe it'll help to find reason of failures/

14
  • You need to share the actual code and test that is failing too please, not just the test result.
    – user740584
    Mar 29, 2014 at 9:06
  • Let us see app/models/user.rb
    – yoppuyoppu
    Mar 29, 2014 at 10:55
  • 1
    Could you add your users factory as well please.
    – user740584
    Mar 29, 2014 at 12:17
  • 1
    As per my answer below, could you go to rails console test, then do user = FactoryGirl.build(:user) then check user.valid? and user.errors.
    – user740584
    Mar 29, 2014 at 13:36
  • 1
    Sorry mate - I'll have just one more attempt... The tests for "signup page" are being run inside the describe "index" block. Is it possible you mean to end the describe "index" block before describe "delete links"? Given you are testing sign up functionality, you probably don't want to sign in before that. Hope that made sense...
    – user740584
    Mar 30, 2014 at 13:24

2 Answers 2

1

It would be nice if we could see how factory :user is defined in your spec/factories.rb as well because you have defined VALID_EMAIL_REGEX differently.

For starters, you could revert the regex condition back to the original:

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

or

see to it that the validation part works with your factory :user.

And remove the second has_secure_password to be on the safe side.

1
  • I removed second has_secure_password and changed VALID_EMAIL_REGEX. But still have the same 4 failures.
    – detskai
    Mar 30, 2014 at 9:39
1

One of the end statements in user_page_spec.rb is misplaced. The describe "index" block should end before describe "profile page".

i.e.

describe "index" do
  ...
  describe "pagination" do
    ...
  end

  describe "delete links" do
    ...
  end
end

describe "profile page" do
  ...
end

describe "signup page" do
  ...
end 

etc.

Signup is for new users registering, so the test should not have a user signed in first.

BTW, you also have describe "delete links" twice, nested inside itself although that wouldn't be the cause of the error; it just repeats a test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.