Я получаю эту ошибку, следуя руководству на railstutorial.org.
Класс модели: $ APPLICATION_HOME / app / models / 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+\-.]+@[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 / app / views / show.html.erb
<% provide :title, @user.name %>
<h1><%= @user.name %></h1>
Файл RSpec: $ APPLICATION_HOME / app / spec / запросы / 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, который я положил в $ APPLICATION_HOME / spec / factories.rb
FactoryGirl.define do
factory :user do
name "amit"
email "amit@gmail.com"
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
Ошибка возникает в этих двух строках
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
Пожалуйста, помогите мне исправить эти ошибки.
Спасибо, Амит Патель