Rspec测试问题没有产生适当的响应

时间:2013-07-31 13:13:09

标签: rspec ruby-1.9.3

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

  
    

user.respond_to?(:名称)

         

应该为“true”但是会出现以下错误

  
(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 宝石'轨道','3.2.13' gem'rspec-rails','2.11.0' 红宝石'1.9.3'

这是来自Hartl教程6.2.2验证存在的精确测试和错误

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

0 个答案:

没有答案