【问题标题】:shoulda-matcher error validating length_ofshoulda-matcher 错误验证 length_of
【发布时间】:2014-02-19 19:04:37
【问题描述】:

我正在为我的用户模型编写一些规范,但测试没有通过。我正在从 gem 的 github 页面闲置this,但没有成功。这是我的代码:

validates :phone, numericality: { only_integer: true }, allow_blank: true, length: { is: 7 }
validates :mobile, numericality: { only_integer: true }, allow_blank: true, length: { is: 10 }

和我的规格

it { should ensure_length_of(:phone).is_equal_to(7) }
it { should ensure_length_of(:mobile).is_equal_to(10) }

此验证应仅在更新过程中运行。

错误:

当电话设置为“xxxxxxx”时,没想到错误会包括“longitud errónea (debe ser de 7 caracteres)”,得到错误:longitud errónea (debe ser 7 个字符)

当 celular 设置为“xxxxxxxxxx”时,没想到错误会包括“longitud errónea (debe ser de 10 caracteres)”,得到错误:longitud errónea (debe s er de 10 个字符)

仅供参考:我设置了西班牙语语言环境;我觉得是这个原因

希望有人可以帮助我。谢谢提前

【问题讨论】:

    标签: activerecord rspec ruby-on-rails-3.2 rspec-rails shoulda


    【解决方案1】:

    我认为您收到了错误,因为 shoulda_matcher 不希望 length: { is: 7 }numericality 一起使用。

    PS 对我来说 - 使用数字作为电话号码很奇怪,您可以使用字符串并使用 validatesformat 来确保有效性数据。例如电子邮件验证here

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    相关资源
    最近更新 更多