I am getting this error while following the tutorial from railstutorial.org.
Model class : $APPLICATION_HOME/app/models/user.rb
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
#validations
validates :name, presence: true, length: { maximum: 50}
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 }
end
View page : $APPLICATION_HOME/app/views/show.html.erb file
<% provide :title, #user.name %>
<h1><%= #user.name %></h1>
RSpec file : $APPLICATION_HOME/app/spec/requests/user_pages_spec.rb
require 'spec_helper'
describe "UserPages" 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
end
Here is factories.rb which I have put in $APPLICATION_HOME/spec/factories.rb
FactoryGirl.define do
factory :user do
name "amit"
email "amit#gmail.com"
password "foobar"
end
end
A snapshot of Gemfile
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails'
end
Here is log I am getting while testing the spec.
Failures:
1) UserPages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `name' for nil:NilClass
# ./app/views/users/show.html.erb:1:in `_app_views_users_show_html_erb__369651065_78543920'
# ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'
2) UserPages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `name' for nil:NilClass
# ./app/views/users/show.html.erb:1:in `_app_views_users_show_html_erb__369651065_78543920'
# ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'
Finished in 0.68212 seconds
12 examples, 2 failures
The error comes at these two lines
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
Please help me to resolve these errors.
Thanks, Amit Patel
It was typo in $APPLICATION_HOME/app/controllers/users_controller.rb
def show
#users = User.find(params[:id])
end
If you notice, the instance variable name is pluralized (#users) and I have used singular name (#user) in the erb template. That is why it is getting failed.
Related
Model Todo
class Todo < ApplicationRecord
has_many :items, dependent: :destroy
validates :title, :created_by, presence: true
end
RSpecs
require 'rails_helper'
RSpec.describe Todo, type: :model do
it { should have_many(:items).dependent(:destroy) }
it { should validate_presence_of(:title) }
it { should validate_presence_of(:created_by) }
end
When i run the command bundle exec rspec, i see:
Finished in 1.82 seconds (files took 0.97238 seconds to load)
5 examples, 2 failures
Failed examples:
rspec ./spec/models/todo_spec.rb:8 # Todo should validate that :title cannot be empty/falsy
rspec ./spec/models/todo_spec.rb:9 # Todo should validate that :created_by cannot be empty/falsy
Can anyone explain why is it failing?
This is the issue in shoulda-matchers. You need to add to your spec_helper.rb:
RSpec.configure do |config|
config.include(Shoulda::Matchers::ActiveModel, type: :model)
config.include(Shoulda::Matchers::ActiveRecord, type: :model)
end
I'm working through the M. Hartl's Rails Tutorial. When running rspec, I keep getting an "unknown attribute followed_id" message. I've looked at the User model and it seems to be fine as well as the user_spec.rb file. Any pointers?
user.rb file:
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "follower_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /[\w+\-.]+#[a-z\d\-.]+\.[a-z]+/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
def feed
Micropost.where("user_id = ?", id)
end
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
and here's the user_spec.rb file:
require 'spec_helper'
describe User do
before { #user = User.new(name: "Example user", email: "user#example.com",
password: "foobar", password_confirmation: "foobar") }
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:remember_token) }
it { should respond_to(:admin) }
it { should respond_to(:authenticate) }
it { should respond_to(:microposts) }
it { should respond_to(:feed) }
it { should respond_to(:relationships) }
it { should respond_to(:followed_users) }
it { should respond_to(:reverse_relationships) }
it { should respond_to(:followers) }
it { should respond_to(:following?) }
it { should respond_to(:follow!) }
it { should respond_to(:unfollow!) }
it { should be_valid }
it { should_not be_admin }
describe "accessible attributes" do
it "should not allow access to admin" do
expect do
User.new(admin: true)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
describe "with admin attribute set to 'true'" do
before do
#user.save!
#user.toggle!(:admin)
end
it { should be_admin }
end
describe "remember token" do
before { #user.save }
its(:remember_token) { should_not be_blank }
end
describe "when name is not present" do
before { #user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { #user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should not be valid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo. foo#bar_baz.com foo#bar+bax.com]
addresses.each do |invalid_address|
#user.email = invalid_address
#user.should_not be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM a_us-ER#f.b.org frst.last#foo.jp a+b#bax.cn first#co.uk]
addresses.each do |valid_address|
#user.email = valid_address
#user.should be_valid
end
end
end
describe "when email is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is empty" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid}
end
describe "when password is equal to nil" do
before { #user.password_confirmation = nil }
it { should_not be_valid }
end
describe "with a password that is too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by_email(#user.email) }
describe "with valid password" do
it { should == found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
describe "remember token" do
before { #user.save }
its(:remember_token) { should_not be_blank }
end
describe "micropost associations" do
before { #user.save }
let!(:older_micropost) do
FactoryGirl.create(:micropost, user: #user, created_at: 1.day.ago)
end
let!(:newer_micropost) do
FactoryGirl.create(:micropost, user: #user, created_at: 1.hour.ago)
end
it "should have the right micropost in order" do
#user.microposts.should == [newer_micropost, older_micropost]
end
it "should be destroyed when users are destroyed" do
microposts = #user.microposts.dup
#user.destroy
microposts.should_not be_empty
microposts.each do |micropost|
Micropost.find_by_id(micropost.id).should be_nil
end
end
describe "status" do
let(:unfollowed_post) do
FactoryGirl.create(:micropost, user: FactoryGirl.create(:user))
end
let(:followed_user) { FactoryGirl.create(:user) }
before do
#user.follow!(followed_user)
3.times { followed_user.microposts.create!(content: "Lorem ipsum") }
end
its(:feed) { should include(newer_micropost) }
its(:feed) { should include(older_micropost) }
its(:feed) { should_not include(unfollowed_post) }
its(:feed) do
followed_user.microposts.each do |micropost|
should include(micropost)
end
end
end
end
describe "following" do
let(:other_user) { FactoryGirl.create(:user) }
before do
#user.save
#user.follow!(other_user)
end
it { should be_following(other_user) }
its(:followed_users) { should include(other_user) }
describe "followed user" do
subject { other_user }
its(:followers) { should include(#user) }
end
describe "and unfollowing" do
before { #user.unfollow!(other_user) }
it { should_not be_following(other_user) }
its(:followed_users) { should_not include(other_user) }
end
end
end
and relationship_spec.rb file:
require 'spec_helper'
describe Relationship do
let(:follower) { FactoryGirl.create(:user) }
let(:followed) { FactoryGirl.create(:user) }
let(:relationship) { follower.relationships.build(followed_id: followed.id) }
subject { relationship }
it { should be_valid }
describe "accessible attributes" do
it "should not allow access to follower_id" do
expect do
Relationship.new(follower_id: follower.id)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
describe "follower methods" do
it { should respond_to(:follower) }
it { should respond_to(:followed) }
its(:follower) { should == follower }
its(:followed) { should == followed }
end
describe "when followed id is not present" do
before { relationship.followed_id = nil }
it { should_not be_valid }
end
describe "when follower id is not present" do
before { relationship.follower_id = nil }
it { should_not be_valid }
end
end
The output after running rspec/:
Failures:
1) User following
Failure/Error: #user.follow!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:199:in `block (3 levels) in <top (required)>'
2) User following followed_users
Failure/Error: #user.follow!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:199:in `block (3 levels) in <top (required)>'
3) User following and unfollowing
Failure/Error: #user.follow!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:199:in `block (3 levels) in <top (required)>'
4) User following and unfollowing followed_users
Failure/Error: #user.follow!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:199:in `block (3 levels) in <top (required)>'
5) User following followed user followers
Failure/Error: #user.follow!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:199:in `block (3 levels) in <top (required)>'
6) User micropost associations status feed
Failure/Error: #user.follow!(followed_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:180:in `block (4 levels) in <top (required)>'
7) User micropost associations status feed
Failure/Error: #user.follow!(followed_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:180:in `block (4 levels) in <top (required)>'
8) User micropost associations status feed
Failure/Error: #user.follow!(followed_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:180:in `block (4 levels) in <top (required)>'
9) User micropost associations status feed
Failure/Error: #user.follow!(followed_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./app/models/user.rb:43:in `follow!'
# ./spec/models/user_spec.rb:180:in `block (4 levels) in <top (required)>'
10) Relationship
Failure/Error: let(:relationship) { follower.relationships.build(followed_id: followed.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:11:in `block (2 levels) in <top (required)>'
11) Relationship when followed id is not present
Failure/Error: let(:relationship) { follower.relationships.build(followed_id: followed.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:29:in `block (3 levels) in <top (required)>'
12) Relationship follower methods
Failure/Error: let(:relationship) { follower.relationships.build(followed_id: followed.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:22:in `block (3 levels) in <top (required)>'
13) Relationship follower methods
Failure/Error: let(:relationship) { follower.relationships.build(followed_id: followed.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:23:in `block (3 levels) in <top (required)>'
14) Relationship follower methods follower
Failure/Error: let(:relationship) { follower.relationships.build(followed_id: followed.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:24:in `block (3 levels) in <top (required)>'
15) Relationship follower methods followed
Failure/Error: let(:relationship) { follower.relationships.build(followed_id: followed.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:25:in `block (3 levels) in <top (required)>'
16) Relationship when follower id is not present
Failure/Error: let(:relationship) { follower.relationships.build(followed_id: followed.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: followed_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:34:in `block (3 levels) in <top (required)>'
Finished in 5.58 seconds
129 examples, 16 failures
Failed examples:
rspec ./spec/models/user_spec.rb:202 # User following
rspec ./spec/models/user_spec.rb:203 # User following followed_users
rspec ./spec/models/user_spec.rb:213 # User following and unfollowing
rspec ./spec/models/user_spec.rb:214 # User following and unfollowing followed_users
rspec ./spec/models/user_spec.rb:207 # User following followed user followers
rspec ./spec/models/user_spec.rb:184 # User micropost associations status feed
rspec ./spec/models/user_spec.rb:186 # User micropost associations status feed
rspec ./spec/models/user_spec.rb:185 # User micropost associations status feed
rspec ./spec/models/user_spec.rb:187 # User micropost associations status feed
rspec ./spec/models/relationship_spec.rb:11 # Relationship
rspec ./spec/models/relationship_spec.rb:30 # Relationship when followed id is not present
rspec ./spec/models/relationship_spec.rb:22 # Relationship follower methods
rspec ./spec/models/relationship_spec.rb:23 # Relationship follower methods
rspec ./spec/models/relationship_spec.rb:24 # Relationship follower methods follower
rspec ./spec/models/relationship_spec.rb:25 # Relationship follower methods followed
rspec ./spec/models/relationship_spec.rb:35 # Relationship when follower id is not present
Randomized with seed 38474
Did you create/run the CreateRelationships migration? Make sure it's correct. If so, try running rake db:test:prepare then re-run your tests.
So I am working through the Ruby on Rails 3 Tutorial. I am currently on section 7.1.3 Testing the User show page using factories.
The code is working and pulls the proper gravatar image however I keep getting an error when running my tests.
Here is the error:
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `downcase' for nil:NilClass
Here is the code from the show.html.erb file:
<% provide(:title, #user.name) %>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
<%= #user.name %>, <%= #user.email %>
Here is the code from the users_helper.rb file:
module UsersHelper
# Returns the Gravatar (http://gravatar.com/) for the given user.
def gravatar_for(user)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
Here is the code from factories.rb file:
FactoryGirl.define do
factory :user do
name "Curtis Test"
email "test#gmail.com"
password "foobar"
password_confirmation "foobar"
end
end
Here is the code from the test file user_pages_spec.rb
require 'spec_helper'
describe "User Pages" do
subject { page }
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 page" do
before { visit signup_path }
it { should have_selector('title', text: full_title('Sign Up')) }
end
end
I discovered my problem. It had nothing to do with FactoryGirl. The problem was in my user model (user.rb), the line that was causing the issue was
before_save { |user| user.email = user.email.downcase! }
The bang after the downcase was causing the email address to be saved as nil since the return of the downcase! is nil. Once I removed that and made the line look like the following it worked just fine.
before_save { |user| user.email = user.email.downcase }
The way I found it was to load the rails console in test environment and tried to create a new user. I noticed that everything was fine but the email was null.
In general, you can debug issues such as this one by referring to the Rails Tutorial sample app reference implementation.
I am getting this error, when starting spec tests. I understand that connection with the user is not set. What shall I do to pass the tests?
RSpec file : posts_controller_spec.rb
require 'spec_helper'
include Devise::TestHelpers
describe PostsController do
render_views
context "Logged in as user" do
let(:user) { FactoryGirl.create(:user) }
before { login_as(user) }
context "on viewing index page" do
let!(:p) { FactoryGirl.create(:post) }
before { get :index }
it { should respond_with(:success) }
it { assigns(:posts).should == [p] }
end
end
end
My view: _home.html.erb
<% #posts.each do |post| %>
<%= link_to post.title, post %>
<%= post.user.full_name %></i></b><hr />
<div>
<%= post.content.html_safe %>
</div>
<% end %>
RSpec failures:
PostsController Logged in as user on viewing index page
Failure/Error: before { get :index }
ActionView::Template::Error:
undefined method `full_name' for nil:NilClass
# ./app/views/posts/_home.html.erb:5:in `block in _app_views_posts__home_html_erb__709569216_93469850'
# ./app/views/posts/_home.html.erb:2:in `each'
# ./app/views/posts/_home.html.erb:2:in `_app_views_posts__home_html_erb__709569216_93469850'
# ./app/views/posts/index.html.erb:1:in `_app_views_posts_index_html_erb___825727830_90641210'
# ./spec/controllers/posts_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
Without seeing your model for Post and User, it looks like you need to change
let!(:p) { FactoryGirl.create(:post) }
to
let!(:p) { FactoryGirl.create(:post, :user => user) }
or
let!(:p) { FactoryGirl.create(:post, :user_id => user.id) }
to correctly set up the association.
However, if your view is assuming that a Post always has a User, maybe your Post model should validate the presence of a User, or maybe you should change your view to handle the case where a Post does not have a User.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm having trouble with Exercise 4 in Chapter 7 of railstutorial.org.
Here are the tests:
describe "signup" do
before { visit signup_path }
describe "with invalid information" do
it "should not create a user" do
expect { click_button "Create my Account".not_to change(User, :count) }
end
end
describe "error messages" do
before { click_button "Create my account" }
it { should have_selector('title', text: "Sign up") }
it { should have_content('error') }
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 do
click_button "Create my account"
end.to change(User, :count).by(1)
end
end
describe "after saving the user" do
before { click_button "Create my account" }
let(:user) { User.find_by_email('user#example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
Here is what it's supposed to test, users_controller.rb:
def create
#user = User.new(params[:user])
if #user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
Here's the show.html.erb code as well:
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
</div>
When I run my tests, I get this:
$ bundle exec rspec spec/requests/user_pages_spec.rb
........FF
Failures:
1) User Pages signup after saving the user
Failure/Error: it { should have_selector('title', text: user.name) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./spec/requests/user_pages_spec.rb:57:in `block (4 levels) in <top (required)>'
2) User Pages signup after saving the user
Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') }
expected css "div.alert.alert-success" with text "Welcome" to return something
# ./spec/requests/user_pages_spec.rb:58:in `block (4 levels) in <top (required)>'
Finished in 0.86152 seconds
10 examples, 2 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:57 # User Pages signup after saving the user
rspec ./spec/requests/user_pages_spec.rb:58 # User Pages signup after saving the user
It should save the test user to the test db, but for some reason, user.name is turning out nil. Any ideas?
Thank you!
Without going over your code in detail to understand the context of everything, it's not that user.name is returning nil, it's that user is nil, and therefore has no method/property name as seen here:
undefined method `name' for nil:NilClass
You have this line here before the test case defining the symbol :user:
let(:user) { User.find_by_email('user#example.com') }
yet you reference the object user in your test:
it { should have_selector('title', text: user.name) }
Change the symbol :user to user in the former and your tests should pass.