railstutorial.orgからのチュートリアルの後にこのエラーが発生します。RSpec:nilのための未定義メソッド `name ':NilClass
Modelクラス:$ APPLICATION_HOME /アプリ/モデル/ user.rb
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
#validations
validates :name, presence: true, length: { maximum: 50}
valid_email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: valid_email_regex },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
end
表示ページ:$ APPLICATION_HOME /アプリ/ビュー/ show.html.erbファイル
<% provide :title, @user.name %>
<h1><%= @user.name %></h1>
RSpecのファイル: $ APPLICATION_HOME /アプリ/仕様/要求/ user_pages_spec.rb
require 'spec_helper'
describe "UserPages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: "Sign up") }
it { should have_selector('title', text: full_title('Sign up')) }
end
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
end
ここでは、私は$アプリケーションシートに入れているfactories.rbですON_HOME /スペック/ factories.rb
FactoryGirl.define do
factory :user do
name "amit"
email "[email protected]"
password "foobar"
end
end
ここGemfile
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails'
end
のスナップショットは、スペックをテストしながら、私は取得していますログです。
Failures:
1) UserPages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `name' for nil:NilClass
# ./app/views/users/show.html.erb:1:in `_app_views_users_show_html_erb__369651065_78543920'
# ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'
2) UserPages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `name' for nil:NilClass
# ./app/views/users/show.html.erb:1:in `_app_views_users_show_html_erb__369651065_78543920'
# ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'
Finished in 0.68212 seconds
12 examples, 2 failures
エラーがこれらのエラーを解決するために私を助けてくださいこの2行
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
でいます。
おかげで、アミットパテル