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
You fill your form with the email=
user@example.combut try to find the user in you db with email=example@railstutorial.org. Make them to be the same.