Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7979401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:46:08+00:00 2026-06-04T09:46:08+00:00

After adding the rspec test provided in Listing 7.32 rspec is having trouble returning

  • 0

After adding the rspec test provided in Listing 7.32 rspec is having trouble returning a User object using the find_by_email method. The rspec test fails with a message “NoMethodError: undefined method name for nil:NilClass” which is essentially telling me it could not find the user and hence no User object was returned. Any insights to why this specific finder method is failing while others work would be helpful.

I added a single user using the info provided at the start of the Chapter 7 Examples section which specifies “Rails Tutorial” for the name and “example@railstutorial.org” for the email. I also changed the rspec have_selector method to match the user’s name in the HTML h1 tag instead of the HTML title tag. The rspec tests pass if I use other finder methods such as User.first or User.find(1) but fails when I use User.find_by_email(“example@railstutorial.org”). If I open a rails console(development mode) I can return the correct user using the find_by_email method. If I open a rails console(test mode) no users are in the database. I ran bundle exec rake db:test:prepare and re-ran it but the rspec test still failed. I can print out the correct results when changing the spec to use User.first and printing out the name and email with pp user.name, user.email but rspec gets lost when attempting to use User.find_by_email(“example@railstutorial.org”).

Environment

ruby 1.9, rails 3.2.3, Ubuntu 10.04

Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.3'
gem 'bootstrap-sass','2.0.0'
gem 'bcrypt-ruby','3.0.1'

group :development, :test do
  gem 'sqlite3'
  gem 'rspec-rails','2.9.0'
  gem 'annotate', '~> 2.4.1.beta'
end

group :test do
  gem 'capybara','1.1.2'
  gem 'factory_girl_rails','1.4.0'
end

#Javascript runtime for Linux
gem 'therubyracer' 

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

User model

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

user_pages rspec(snippet)

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: "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 }
      let(:user) { User.find_by_email("example@railstutorial.org") }
      it { should have_selector('h1', text: user.name) #error with name on NilClass!
      it { should have_selector('div.alert.alert-success', text: 'Welcome')}
    end
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:46:10+00:00Added an answer on June 4, 2026 at 9:46 am

    You fill your form with the email=user@example.com but try to find the user in you db with email=example@railstutorial.org. Make them to be the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble figuring out how to maintain my table stripes after adding new
After adding elements to page using something like this, $('#blah').append('<div id=bugsbunny></div>'); how can I
I'm using aspxgridview and after adding operation by using RowInserting event and I don't
After adding plone.app.async, I cannot start my production instances normally using 'bin/instance start'. However,
After adding a boolean attribute to an object, fetching that object from the datastore
I am using iAd in my iphone 4.0 application but after adding the ADBannerView
After adding a ribbon item, or calling a method that updates an item, the
Edit: Solved, I wasn't using 'validate()' after adding components. I have a GUI class
I'm creating a PDF using iTextSharp and after adding all my pages, I want
I'm creating a PDF using iTextSharp and after adding all my pages, I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.