gpt4 book ai didi

ruby - 验证 ActiveRecord 时,我得到有效? => false 但 errors.count 中没有错误。怎么来的?

转载 作者:数据小太阳 更新时间:2023-10-29 07:29:39 25 4
gpt4 key购买 nike

在 rspec 中创建 ActiveRecord 时,我使用固定装置来获取有效记录。但是当在测试中使用 fxitures 时,它们似乎无法通过验证。在以下示例中,该员工似乎完全有效,但规范中的相关验证表明它们无效。

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

例如:

describe SessionsController do
fixtures :users, :employees

describe "Logging in by cookie" do
def set_remember_token token, time
@user[:remember_token] = token;
@user[:remember_token_expires_at] = time
debugger
@user.save! # THIS SAYS THE EMPLOYEE IS INVALID, but why, if the fixtures are good?
end
it 'logs in with cookie' do
stub!(:cookies).and_return({ :auth_token => 'hello!' })
logged_in?.should be_true
end
end
end

为什么我会收到验证失败的任何想法?(当然,我把中间的代码删掉了很多,有可能错误完全在别的地方)

最佳答案

您可以在 .save 之后调试 @user(没有爆炸声!),这应该会添加错误,它可能是由模型回调或您的验证方法所做的事情引起的。在您的测试中,只需执行以下操作:

@user.save
puts @user.errors.inspect # or your command line debugger

并在运行测试时读取输出。

关于ruby - 验证 ActiveRecord 时,我得到有效? => false 但 errors.count 中没有错误。怎么来的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/951277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com