Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails规范错误:预期有效?返回真,得到假_Ruby On Rails_Ruby On Rails 4_Rspec Rails - Fatal编程技术网

Ruby on rails Rails规范错误:预期有效?返回真,得到假

Ruby on rails Rails规范错误:预期有效?返回真,得到假,ruby-on-rails,ruby-on-rails-4,rspec-rails,Ruby On Rails,Ruby On Rails 4,Rspec Rails,我正在学习Michael Hartl的RoR教程,但在测试代码时出现了以下错误。我似乎不明白为什么我的对象不能作为有效对象通过 My user.rb-file: class User < ActiveRecord::Base before_save { |user| user.email = user.email.downcase } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_R

我正在学习Michael Hartl的RoR教程,但在测试代码时出现了以下错误。我似乎不明白为什么我的对象不能作为有效对象通过

My user.rb-file:

class User < ActiveRecord::Base

  before_save { |user| user.email = user.email.downcase }

  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 }
end
当我运行测试时,出现以下错误: 失败:

  1) User 
     Failure/Error: it { should be_valid }
       expected valid? to return true, got false
     # ./spec/models/user_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.21355 seconds
9 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:12 # User `
当我在rails控制台中运行user_with_same_email.errors时,我得到以下答案:

ruby-2.0.0-p247 :001 > user = User.new(name: "bobo", email: "user@example.com")
 => #<User id: nil, name: "bobo", email: "user@example.com", created_at: nil, updated_at:     nil> 
ruby-2.0.0-p247 :002 > user_with_same_email =  user.dup
 => #<User id: nil, name: "bobo", email: "user@example.com", created_at: nil, updated_at: nil> 
ruby-2.0.0-p247 :003 > user_with_same_email.valid?
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") =     LOWER('user@example.com') LIMIT 1
 => true 
ruby-2.0.0-p247 :004 > user_with_same_email.errors
 => #<ActiveModel::Errors:0x007fad6491f780 @base=#<User id: nil, name: "bobo", email:    "user@example.com", created_at: nil, updated_at: nil>, @messages={}> 
ruby-2.0.0-p247 :005 >
如您所见,错误不会传递任何消息

有人知道我如何修正这个错误吗

谢谢

增加:

当我试图用已保存的电子邮件保存用户时,似乎会出现此错误

$ rails console --sandbox
Loading development environment in sandbox (Rails 4.0.0)
Any modifications you make will be rolled back on exit
ruby-2.0.0-p247 :001 > user = User.new(name: "bobo", email: "user@example.com")
 => #<User id: nil, name: "bobo", email: "user@example.com", created_at: nil, updated_at:         nil> 
ruby-2.0.0-p247 :002 > user.save
   (0.1ms)  SAVEPOINT active_record_1
  User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") =     LOWER('user@example.com') LIMIT 1
  SQL (3.3ms)  INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES       (?, ?, ?, ?)  [["created_at", Wed, 16 Oct 2013 10:15:19 UTC +00:00], ["email",     "user@example.com"], ["name", "bobo"], ["updated_at", Wed, 16 Oct 2013 10:15:19 UTC +00:00]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
 => true 
    ruby-2.0.0-p247 :003 > user_with_same_email =  user.dup
 => #<User id: nil, name: "bobo", email: "user@example.com", created_at: nil, updated_at: nil> 
ruby-2.0.0-p247 :004 > user_with_same_email.save
   (0.1ms)  SAVEPOINT active_record_1
  User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('user@example.com') LIMIT 1
   (0.1ms)  ROLLBACK TO SAVEPOINT active_record_1
 => false 
ruby-2.0.0-p247 :005 > 
我真的不知道为什么会这样。我的验证不应该解决这个问题吗


谢谢

您传递的名称少于50个字符,您的电子邮件与您创建的正则表达式匹配。唯一剩下的就是电子邮件的独特性。我打赌你没有在不同规格之间正确清理数据库。你能通过扩展你的有效规格来确认吗

it "is valid" do
  subject.valid?
  subject.errors.full_messages.should eq []
end
打电话有效吗?将填充错误数组,然后规范将失败并显示错误消息


如果电子邮件已被接收,请尝试使用数据库\u cleaner。在使用之前,您必须将其添加到GEM文件中。

如前所述,通过@user.errors.messages发送的错误消息包含{:email=>[已被接收]}

您可以通过在RSpec.configure do | config |块中的spec_helper.rb中添加以下内容来初步解决此问题:

但我不相信这个解决方案。看起来,用户在测试用例之后不会销毁自己。

我的测试也失败了

  it { should be_Valid }
user.valid上的我的rails控制台会话为true

更正了下面的测试

it { should be_valid }

大写字母“be_Valid”是问题所在

尝试在控制台中手动执行测试,然后检查user.errors的值,直到有点像新手;如何检查user.errors的值?打开控制台rails c,键入user=user.newname:bobo,电子邮件:user@example.com从上面的测试,然后是user.valid?它将返回false,然后返回user.errors,这将输出阻止项目有效的错误。有人知道我缺少什么吗?几天来一直在断断续续地解决此问题,但由于某些原因,错误仍然存在。首先隔离:在保存之前删除-它是否仍会给出错误?如果是这样,请尝试逐个删除这两个验证行中的每一行,并查看是否出现错误。
  it { should be_Valid }
it { should be_valid }