【发布时间】:2010-05-13 05:01:28
【问题描述】:
我有一个 User 模型,并在其上使用 acts_as_authentic(来自 authlogic)。我的 User 模型在 username 上有 3 个验证,如下所示:
User < ActiveRecord::Base
acts_as_authentic
validates_presence_of :username
validates_length_of :username, :within => 4..40
validates_uniqueness_of :username
end
我正在编写一个测试来查看我的验证是否有效。不知何故,在验证名称的唯一性时,我得到了 2 个错误而不是 1 个。要查看过多的错误,我进行了以下测试:
describe User do
before(:each) do
@user = Factory.build(:user)
end
it "should have a username longer then 3 symbols" do
@user2 = Factory(:user)
@user.username = @user2.username
@user.save
puts @user.errors.inspect
end
end
username:@errors={"username"=>["has already been taken", "has already been taken"]} 出现 2 个错误。
另一个问题是我将用户名设置为nil。不知何故,我得到了四个验证错误而不是三个:@errors={"username"=>["is too short (minimum is 3 characters)", "should use only letters, numbers, spaces, and .-_@ please.", "can't be blank", "is too short (minimum is 4 characters)"]}
我认为 authlogic 是导致这种奇怪行为的原因。但我什至无法想象如何解决这个问题。有什么想法吗?
【问题讨论】:
标签: ruby-on-rails validation authlogic