问题
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!
EDIT: Here is a link to the repo, if anyone would like to sift through it and/or replicate the error. https://github.com/FluxAnimus/sample_app/tree/sign-up
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 "michael@example.com"
password "foobar"
password_confirmation "foobar"
end
end
app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
end
end
回答1:
Assuming you're using the definition of gravatar_for
in the tutorial, downcase
is only referenced in the context of user.email.downcase
which means that user
is defined, but the email
method/attribute of user
returns nil
. You can also go by the filename, method and line number provided in the stack trace to identifying the offending code.
Update: In your User
model you, had a before_save
call which included the following:
self.email = email.downcase!
Since downcase!
returns nil
, this has the effect of setting email
to nil
. Per the tutorial, you can use either of the following instead:
self.email.downcase!
or:
self.email = email.downcase
回答2:
You have a variable that you think is a string, but it's actually nil
.
You can't call downcase
on nil
, so you're getting this error.
Welcome to Ruby, you'll be seeing that one a lot. :)
EDIT
I see you've added some code to the question.
As the error message says, it's user_helper.rb line 5:
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
It's user.email
that is nil
. I'm guessing that your FactoryGirl factory isn't assigning an email address to the user.
来源:https://stackoverflow.com/questions/18006056/rspec-error-undefined-method-downcase-for-nilnilclass