Ruby 验证ActiveRecord时,I';我的有效期到了=&燃气轮机;false,但errors.count中没有错误。怎么会?
在rspec中创建ActiveRecord时,我使用fixture作为有效记录。 但是当在测试中使用fxitures时,它们似乎无法通过验证。 在下面的示例中,员工似乎完全有效,但规范中的相关验证表明他们无效Ruby 验证ActiveRecord时,I';我的有效期到了=&燃气轮机;false,但errors.count中没有错误。怎么会?,ruby,activerecord,rspec,Ruby,Activerecord,Rspec,在rspec中创建ActiveRecord时,我使用fixture作为有效记录。 但是当在测试中使用fxitures时,它们似乎无法通过验证。 在下面的示例中,员工似乎完全有效,但规范中的相关验证表明他们无效 class Employee < ActiveRecord::Base validates_presence_of :email end class User < ActiveRecord::Base validates_associated :em
class Employee < ActiveRecord::Base
validates_presence_of :email
end
class User < ActiveRecord::Base
validates_associated :employee
end
#Command line debugger in 'debugger' (below) returns:
Employee.find(745185059).errors.count # => 0
Employee.find(745185059).errors.full_messages # => []
Employee.find(745185059).valid? # => true
知道我为什么验证失败了吗?
(当然,我删掉了很多中间代码,可能错误完全在其他地方)您可以在.save之后调试@user(没有爆炸!),这应该会添加错误,可能是由于模型回调或您的validate方法造成的。在测试中,只需执行以下操作:
@user.save
puts @user.errors.inspect # or your command line debugger
并在运行测试时读取输出。这就成功了!我不知道,即使尝试Employee(123).send(validate)也不起作用。多谢你抽出时间,奥马尔!
@user.save
puts @user.errors.inspect # or your command line debugger