0

当我尝试运行 rspec 测试时,我收到以下响应。

user.respond_to?(:name)

应该得到“真”,但得到以下错误

(added)    **NameError: undefined local variable or method `user' for main:Object
from (irb):2
from /Users/mwsage/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/mwsage/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/mwsage/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

而不是接收“真”或“假”响应。有任何想法吗?

来自 Gemfile gem 'rails', '3.2.13' gem 'rspec-rails', '2.11.0' ruby​​ '1.9.3'

这是来自 Hartl 教程 6.2.2 Validating Presence 的确切测试和错误

user = User.new(name: "", email: "mhartl@example.com")
>> user.save
=> false

*false 是我应该得到的响应,上面的错误是我收到的。

这是我的 user.rb 文件

class User < ActiveRecord::Base 
  attr_accessible :name, :email

  validates :name, presence: true
end

这是我的 user_spec.rb 文件

require 'spec_helper'

describe User do

   before { @user = User.new(name: "Example User", email: "user@example.com") }

   subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
end
4

0 回答 0