I'm copying this directly from Stack Overflow to see if anyone can help. Here is the link to my question on Stack Overflow:
http://stackoverflow.com/questions/18006056/rspec-error-undefined-method-downcase-for-nilnilclass
I keep getting this error when running my Rspec test and
can't figure out why. I'm pretty new to Rails (and programming in
general) so any direction or help would be much appreciated!
Failures:
1) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `downcase' for nil:NilClass
# ./app/helpers/users_helper.rb:5:in `gravatar_for'
# ./app/views/users/show.html.erb:3:in `_app_views_users_show_html_erb__1766857043046396980_38603940'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
2) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `downcase' for nil:NilClass
# ./app/helpers/users_helper.rb:5:in `gravatar_for'
# ./app/views/users/show.html.erb:3:in `_app_views_users_show_html_erb__1766857043046396980_38603940'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.46129 seconds
39 examples, 2 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:12 # User pages profile page
rspec ./spec/requests/user_pages_spec.rb:11 # User pages profile page
I'm in Chapter 7.13 of the Ruby on Rails tutorial. The tests cleared
just fine until I added the Gravatar code and FactoryGirls Gem.
/app/helpers/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
spec/requests/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_content(user.name) }
it { should have_title(user.name) }
end
.
.
.
end
And the last reference: app/views/users/show.html.erb
<% provide(:title, @user.name) %>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
Here is the factory file:
FactoryGirl.define do
factory :user do
name "Michael Hartl"
email "mic...@example.com"
password "foobar"
password_confirmation "foobar"
end
end