Ruby on rails 铁路";更新“U属性”;跳过验证?

Ruby on rails 铁路";更新“U属性”;跳过验证?,ruby-on-rails,rspec,Ruby On Rails,Rspec,我有以下简化模型: class User attr_accessible :password validates :password, :presence => true validates_confirmation_of :password, :length => { :minimum => 4 } end 这很直截了当。我正在用RSpec/FactoryGirl进行测试,并进行以下测试: it "cannot create with password <

我有以下简化模型:

class User
  attr_accessible :password
  validates :password, :presence => true
  validates_confirmation_of :password, :length => { :minimum => 4 }
end
这很直截了当。我正在用RSpec/FactoryGirl进行测试,并进行以下测试:

it "cannot create with password < 4 characters" do
  expect{ model = FactoryGirl.create(:user, :password => "12", :password_confirmation => "12") }.to raise_error
end

it "cannot update to password < 4 characters" do
  model = FactoryGirl.create(:user)
  model.should be_valid
  model.update_attributes({:password => "12", :password_confirmation => "12"})
  model.reload
  model.password.should_not eq("12")
end
它“无法使用小于4个字符的密码创建”是否执行
期望{model=FactoryGirl.create(:user,:password=>“12”,:password\u confirmation=>“12”)}。引发\u错误
结束
它“无法更新为小于4个字符的密码”
model=FactoryGirl.create(:user)
model.0应该是有效的
model.update_属性({:password=>“12”,:password_confirmation=>“12”})
模型。重新加载
model.password.不应等于(“12”)
结束

然而,第二次测试失败了。出于某种原因,“12”似乎可以作为有效密码保存到数据库中。为什么?API声明,
update\u属性
不应绕过任何验证

我不认为它跳过了验证

我怀疑:

model.update_attributes({:password => "12", :password_confirmation => "12"})
返回值为false。Password可能不是数据库中的一列,而是您可能已经加密了\u Password和Password\u salt,因此重新加载它不会从数据库中读取密码(这将很糟糕)

此外,您可能需要:

attr_accessible :password, :password_confirmation

好的,我检查过了,你是对的。我在测试中添加了
model.save
,这会抛出一个错误。我担心重载实际上并没有更新脏字段。这似乎是不受欢迎的行为(如中所示,字面上与我期望的重载操作相反),但这是一个单独的问题。您还可以执行model.update\u attributes!它会提高而不是返回真或假。啊,这确实有道理。谢谢你的跟进!