我有一个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
上遇到了2个错误:@errors={"username"=>["has already been taken", "has already been taken"]}
。
另一个问题是我将用户名设置为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会导致这种奇怪的行为。但我甚至无法想象如何解决这个问题。有什么想法吗?