2013-07-30 54 views
0

我有一个用户模型,需要至少有一个大写字母的密码。我的规格写成如下:shoulda matchers with自定义验证缺陷

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 

回答

0

我只能说你执行的代码比你出什么不同。您的RSpec失败消息表明您期待errors数组包含一个包含字符串的数组,而不是期望它包含字符串本身。难道这个错误信息是由于运行了包含....with_message(["..."])的规范而不是你显示的内容而产生的?

相关的shoulda代码非常简单。请参阅https://github.com/thoughtbot/shoulda-matchers/blob/d680e7d47ba0d0bc6eba888cc4648f65b61ac2cf/lib/shoulda/matchers/active_model/allow_value_matcher.rb的行120

+0

我添加了验证的代码,负责生成此错误消息。那里有什么新奇的东西? – user2287662

+0

您需要将其作为代码添加到您的问题中,以使其可读。 –

+0

对不起,现在应该修复。 – user2287662