#15 needs-git-fork
Karl Smith

Test fails with :allow_blank and :allow_nil are true

Reported by Karl Smith | February 7th, 2008 @ 01:51 PM

In model:

validates_length_of :middle_initial, :is => 1, :allow_blank => true

Will fail test with:

should_ensure_length_in_range :middle_initial, (1..1)

should_ensure_length_in_range :middle_initial, (0..1)

Reason:

should_ensure_length_in_range will test the attribute with a zero length string, which is valid when :allow_blank is set to true. And skips lengths less than zero, as in the second case.

Comments and changes to this ticket

  • carlivar
  • Tammer Saleh

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

    • → State changed from “new” to “needs-git-fork”
    • → Tag changed from “” to “should_require_unique_attributes”
    • → Assigned user cleared.
  • Chris O'Sullivan

    Chris O'Sullivan November 20th, 2008 @ 05:25 AM

    Is this still an issue?

    I created a test for this in this branch: http://github.com/thechrisoshow/... and it passes.

    Have a look and see if I'm missing the point.

  • Karl Smith

    Karl Smith November 20th, 2008 @ 11:07 AM

    • → Tag changed from “should_require_unique_attributes” to “should_ensure_length_in_range should_ensure_length_is”

    Chris, I couldn't find your specific test, but test still fail when :allow_blank => true. Created new rails app, cloned your branch:

    class User < ActiveRecord::Base validates_length_of :middle_initial, :is => 1, :allow_blank => true

    end

    class UserTest < ActiveSupport::TestCase should_ensure_length_in_range :middle_initial, (1..1) end

    1) Failure: test: User should not allow middle_initial to be less than 1 chars long. (UserTest) User allowed "" as a value for middle_initial.

    is not true.

    class UserTest < ActiveSupport::TestCase should_ensure_length_is :middle_initial, 1 end

    1) Failure: test: User should not allow middle_initial to be less than 1 chars long. (UserTest) User allowed "" as a value for middle_initial.

    is not true.

    Let me know if there is anything else I can help with.

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 »