#26 needs-git-fork
Tammer Saleh

AR tests should use AR default messages

Reported by Tammer Saleh | March 17th, 2008 @ 09:39 AM

The macros for AR currently contain hardcoded regex for the validation messages (like opts[:message] ||= /invalid/). These should be changed to default to a lookup into the AR hash of default validation messages.

Comments and changes to this ticket

  • bhauff

    bhauff June 10th, 2008 @ 12:24 PM

    I have run in to this problem too I believe. I have a float field in my database, and have added the test:

    should_not_allow_values_for :longitude, "One", "Two Hundred"

    In my model I have:

    validates_numericality_of :longitude

    I get the following error when testing:

    /invalid/ not found in ["is not a number"] when set to "One".

    /invalid/ not found in ["is not a number"] when set to "Two Hundred".

  • bhauff

    bhauff June 10th, 2008 @ 12:29 PM

    I was able to get the tests to pass by adding:

    {:message => "is not a number"}

    to the

    should_not_allow_values_for :longitude, "One", "Two Hundred"

    test.

  • Tammer Saleh

    Tammer Saleh June 10th, 2008 @ 12:55 PM

    This is really a different issue. If shoulda used the builtin hashes, it would still be checking for a particular value that wouldn't match "Numeric". The use of :message to solve this problem is the right way to go.

  • Alain Ravet

    Alain Ravet June 23rd, 2008 @ 06:35 AM

    I concur the current behaviour - check against a harcoded regexp fragment - is a problem for any non-English speaking sites.

    Rails let's you dry-ly define in one place all the error messages for your target language, but shoulda forces you to duplicate code like :

      should_require_attributes :name, :message => ActiveRecord::Errors.default_error_messages[:blank]
    

    :message should be required for non-standard messages, not for the default ones.

  • Tammer Saleh

    Tammer Saleh June 26th, 2008 @ 04:58 PM

    • → State changed from “new” to “needs-git-fork”
    • → Tag changed from “” to “active_record_helper should_require_attributes”
    • → Assigned user cleared.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »