RSpec:nil的未定义方法`name':NilClass

时间:2012-02-17 13:39:19

标签: ruby ruby-on-rails-3 rspec factory-bot

我在遵循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 / requests / 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

这是我放在$ APPLICATION_HOME / spec / factories.rb中的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) }

请帮我解决这些错误。

谢谢,Amit Patel

1 个答案:

答案 0 :(得分:4)

$ APPLICATION_HOME / app / controllers / users_controller.rb

中的拼写错误
  def show
    @users = User.find(params[:id])
  end

如果您注意到,实例变量名称是复数(@users),我在erb模板中使用了单数名称(@user)。这就是它失败的原因。