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.
Related
I'm in the Hartl tutorial in Chapter 5.
I'm getting the following error when running the test file:
Failures:
1) User pages Signup page
Failure/Error: it { should have_selector('title', text: full_title('Sign up')) }
NoMethodError:
undefined method `full_title' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0xaeb792c>
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
For brevity, I've commented out the other "full_title not found" errors while troubleshooting.
I've confirmed that the method is in the app/helpers/application_helper.rb file.
Any ideas why it's not being found? It's most definitely in the helper file.
My user pages spec file:
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
end
and my application_helper.rb file
module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
end
The Tutorial seems to add that in a different place that yours. The full_title used on the spec was defined on spec/support/utilities.rb. The one defined on the application_helper.rb was used in app/views/layouts/application.html.erb.
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.
so I've been using Michael Hartl's tutorial for some time and I can say it's really useful but there's a problem and I gues it's not on the tutorial's part. So in chapter "9.2.2 Requiring the right user" ther's a test for checking that a user can access neither other user's edit page nor submit a direct PUT reauest.
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong#example.com") }
before { sign_in user }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_selector('title', text: full_title('Edit user')) }
end
describe "submitting a PUT request to the Users#update action" do
before { put user_path(wrong_user) }
specify { response.should redirect_to(root_path) }
end
end
So long all seems right but the test fails:
1) Authentication authorization as wrong user submitting a PUT request to the Users#update action ←[31mFailure/Error:←[0m ←[31mspecify { response.should redirect_to(root_path }←[0m←[31mExpected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>←[0m←[36m # ./spec/requests/authentication_pages_spec.rb:107:in `block (5 levels) in <top (required)>'←[0m
Here's the User controller:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update]
before_filter :correct_user, only: [:edit, :update]
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(params[:user])
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(params[:user])
flash[:success] = "Profile updated"
sign_in #user
redirect_to #user
else
render 'edit'
end
end
private
def signed_in_user
unless signed_in?
puts "No user signed in"
store_location
redirect_to signin_path, notice: "Please sign in."
end
end
def correct_user
#user = User.find(params[:id])
puts "Incorrect user" unless current_user?(#user)
redirect_to(root_path) unless current_user?(#user)
end
end
So as you can see the problem is that when using RSpec put method, the test fails even before checking for the right user because it sees ther's no user signed in.
This is a small problem which can easily be omitted (incorrect user cannot make direct PUT request anyway) but it's a puzzle for me why doesn't it work correct and I can't get the answer for quite a time already.
It looks like the signed_in_user filter is redirecting back to the sign in page before the correct_user fires. That suggests that the user is not actually signed in correctly by the sign_in user call in the before block.
Have you defined sign_in in spec/support/utilities.rb?
include ApplicationHelper
def sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token
end
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.
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.