Skip to content

numericality matcher should not raise an IneffectiveTestError #832

Closed
@ineu

Description

validates_numericality matchers was changed with assumption that validations are there for data consistency only. But for data consistency one uses proper database column types and DB-level constraints. Validations are perfect for users.

For example, I use validates_numericality on all my columns that can be updated from forms. I want my users to see 'not a number' when they type non-number in form input. Following code recommends to remove validation which is definitely not something I want to do: https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb#L522

Also, behavior described in #784 (comment) is not really a case. Please see following snippet:

[25] pry(main)> a = Payment.new
=> #<Payment:0x00562d6b28fcf0 id: nil, account_id: nil, packet_id: nil, payment_type_id: nil, deposit: nil, amount: nil, operator_id: nil, created_at: nil>
[26] pry(main)> a.amount = 'foo'
=> "foo"
[27] pry(main)> a.amount
=> #<BigDecimal:562d6a0cb9d8,'0.0',9(18)>
[28] pry(main)> a.valid?
=> false
[29] pry(main)> a.errors
=> #<ActiveModel::Errors:0x00562d69e84530
 @base=
  #<Payment:0x00562d6b28fcf0
   id: nil,
   account_id: nil,
   packet_id: nil,
   payment_type_id: nil,
   deposit: nil,
   amount: #<BigDecimal:562d6a0cb9d8,'0.0',9(18)>,
   operator_id: nil,
   created_at: nil>,
 @messages={:account=>["can't be blank"], :packet=>["can't be blank"], :payment_type=>["can't be blank"], :deposit=>["can't be blank"], :amount=>["is not a number"]}>

As you can see, numeric validation does add 'not a number' error, despite 'amount' is decimal column. So it matters and should neither be removed nor should raise IneffectiveTestError exception.

I think there should be some kind of configuration option to disable IneffectiveTestError exception. Using validations to get nice error messages is valid and, I think, frequent use case.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions