hartl chapter 5 full_title not found - ruby

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.

Related

Ruby on Rails 3 Tutorial gravatar_for test error

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.

RSpec: ActionView::Template::Error: undefined method `full_name' for nil:NilClass

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.

Rails Tutorial Chapter 7, Exercise 4 [closed]

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.

RSpec : undefined method `name' for nil:NilClass

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.

Using RSpec2 to test a controller's show action

I have a fairly simple Rails 3 project where I've defined a custom route:
get 'factions/:name' => 'factions#show', :as => :factions
get 'factions' => 'factions#index'
... which when running rails s gives me the expected page (http://localhost:3000/factions/xyz is HTTP 200 with the app/views/factions/show.html.haml being displayed). However I've tried multiple different ways of expressing a spec that will work, below is my latest incarnation:
require 'spec_helper'
describe FactionsController do
render_views
describe "GET 'show'" do
before { get '/xyz' }
subject { controller }
it { should respond_with(:success) }
it { should render_template(:show) }
end
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
end
end
The GET 'index' spec passes without complaint but no matter what I do the GET 'show' specs cannot pass - even if they do succeed when I am browsing to them locally.
1) FactionsController GET 'show'
Failure/Error: before { get '/xyz' }
ActionController::RoutingError:
No route matches {:controller=>"factions", :action=>"/xyz"}
# ./spec/controllers/factions_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
The action really should be show but my routes.rb configuration must be incorrect or something. What gives?
(Additional context: I am using bundle exec spork for speeding up my specs and I have restarted the spork server multiple times just to make sure I'm not completely insane.)
Change:
before { get '/xyz' }
To:
before { get :show, :name => 'xyz' }

Resources