关于Rails上的ruby:如何在更新密码时测试密码长度

how to test password length on update of password

创建新用户时,将为其提供密码。

我想测试用户更新或更改密码的情况。

User.rb

1
2
3
4
5
6
 validates :password, :on => :create,
                :presence => true,
                :confirmation => true,
                :length => {:within => 6..12}

before_validation :make_password, :on => :create

在spec / models / user_spec.rb中,我具有以下内容:

描述"密码验证"做
之前(:每个)做
@用户=工厂(:用户)
结束

1
2
3
4
it"should reject passwords that are too long" do
  too_long_password ="a" * 13
  @user.update_attributes(@attr.merge(:password => too_long_password, :password_confirmation => too_long_password)).should_not be_valid
end

end

不起作用。
现在如何测试更新?
任何想法表示赞赏。


从验证中删除:on => :create子句。删除此选项将在创建和更新期间激活验证。

1
2
3
 validates :password,:presence => true,
                     :confirmation => true,
                     :length => {:within => 6..12}

您没有提到,但也可以删除:on => :create,如果在before_validation中也需要它的话