Rspec 错误,“nil:NilClass 的未定义方法‘小写’"; [英] Rspec error, "undefined method 'downcase' for nil:NilClass"
问题描述
我在运行 Rspec 测试时不断收到此错误,但不知道为什么.我对 Rails(以及一般的编程)很陌生,因此非常感谢任何指导或帮助!
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!
这里是存储库的链接,如果有人想对其进行筛选和/或复制错误. 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
我在 Ruby on Rails 教程的第 7.13 章.在我添加 Gravatar 代码和 FactoryGirls Gem 之前,测试一切顺利.
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 文件
/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
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
最后一个参考:app/views/users/show.html.erb
And the last reference: app/views/users/show.html.erb
<% provide(:title, @user.name) %>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
这是工厂文件:
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
app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
end
end
推荐答案
假设你在教程中使用了 gravatar_for
的定义,downcase
只在user.email.downcase
的上下文,这意味着 user
是 定义的,但是 email
方法/属性 user
返回 nil
.您还可以通过堆栈跟踪中提供的文件名、方法和行号来识别有问题的代码.
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.
更新:在您的 User
模型中,您有一个 before_save
调用,其中包括以下内容:
Update: In your User
model you, had a before_save
call which included the following:
self.email = email.downcase!
由于downcase!
返回nil
,这具有将email
设置为nil
的效果.根据教程,您可以使用以下任一方法:
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!
或:
self.email = email.downcase
这篇关于Rspec 错误,“nil:NilClass 的未定义方法‘小写’";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!