我在運行我的Rspec測試時不斷收到此錯誤,但無法找出原因。我對Rails很陌生(一般編程),所以任何方向或幫助將非常感謝!Rspec錯誤,「未定義方法'downcase'爲零:NilClass」
編輯:這裏是回購的鏈接,如果有人想篩選它和/或複製錯誤。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
而最後的參考:應用程序/視圖/用戶/ show.html.erb
<% provide(:title, @user.name) %>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
這裏是工廠的文件:
FactoryGirl.define do
factory :user do
name "Michael Hartl"
email "[email protected]"
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
是的,我正在使用教程中的定義。我已經添加了堆棧跟蹤中的頁面。仍似乎無法找到問題。再一次,我的道歉,我幾乎是全新的。我確信我在這一點上失去了一些顯而易見的東西。值得注意的是,當我啓動應用程序時,一切似乎都正常工作。 –
只需要拋出一些東西:a)如果你正在使用guard/spork,你重新啓動了它嗎?b)你的測試數據庫是否是最新的(例如''rake test:prepare'),c)你的UsersController#show'方法是否正確定義? –
我實際上並沒有使用警衛/ spork,但我在Gemfile中包含了Gem。我的測試運行速度非常快,因此我決定不使用它們。我用我的用戶控制器顯示文件更新了這個問題。看起來是對的,但我肯定是錯的。 –