我有一个用户模型,它需要一个至少包含一个大写字母的密码。我的规范是这样写的:
subject { User.new }
it { should_not allow_value("test123$").for(:password).with_message("password requires an uppercase letter (\"test123$\")") }
但是,此规范返回以下失败:
Expected errors to include ["password requires an uppercase letter (\"test123$\")"] when password is set to "test123$", got errors: ["password requires an uppercase letter (\"test123$\")"]
在我看来,错误数组显然包含正确的消息。我在这里做错了什么?
编辑:这是我试图指定的验证:
[
[/[A-Z]/, "requires an uppercase letter"],
[/[a-z]/, "requires a lowercase letter"],
[/[0-9]/, "requires a number"],
[/[^\w]/, "requires a special character"]
].each do |(format, message)|
validates :password, :format => {:with => format,
:message => message},
:allow_blank => true
end