运行我的 Rspec 测试时,我不断收到此错误,但不知道为什么。我对 Rails(和一般编程)很陌生,所以任何方向或帮助将不胜感激!
编辑:如果有人想筛选和/或复制错误,这里是回购的链接。 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 之前,这些测试都顺利完成。
/app/helpers/users_helper.rb 文件
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
规范/请求/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
<% 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
应用程序/控制器/users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
end
end