所以我正在研究 Ruby on Rails 3 教程。我目前在第 7.1.3 节使用工厂测试用户显示页面。
该代码正在运行并提取正确的 gravatar 图像,但是在运行测试时我不断收到错误消息。
这是错误:
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `downcase' for nil:NilClass
以下是 show.html.erb 文件中的代码:
<% provide(:title, @user.name) %>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
<%= @user.name %>, <%= @user.email %>
这是 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
这是 factory.rb 文件中的代码:
FactoryGirl.define do
factory :user do
name "Curtis Test"
email "test@gmail.com"
password "foobar"
password_confirmation "foobar"
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_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