Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add assert_not_valid to model assertions

#117

Rails has had an assert_valid method for a very long time, but its corresponding opposite method never existed in Rails core. I'm submitting Pivotal Labs's implementation of this method.

This method proves to be a very useful pattern for testing various kinds of invalid ActiveRecord objects.

Reported by Nathan Wilmes · May 5th, 2008 @ 08:01 PM

State: invalid
Milestone: none
Assigned to: josh josh
Importance: none

Activity

  1. Tarmo Tänav
    Tarmo Tänav

    Why not make the messages parameter optional and not assert the specific contents of the validation errors unless it is provided?

    May 5th, 2008 @ 09:21 PM

  2. Nathan Wilmes
    Nathan Wilmes
    • Title changed from [patch] Add assert_not_valid to model assertions to Add assert_not_valid to model assertions

    I don't have a strong problem with that. I'd argue that this function is most useful as a way to confirm which errors you've received, but you could certainly use it for a more basic valid/not valid checker.

    May 5th, 2008 @ 11:38 PM

  3. Tarmo Tänav
    Tarmo Tänav

    A couple of more questions:

    1. Why not "assert_invalid"?

    2. Have you thought about supporting partial error messages checks? Basically, if you have a test for a particular validation then would it not make sense to only check for the presence of that validation error and not pollute the test for one validation with the failure of some other unrelated validation? Though I have no idea what kind interface changes this may require in order to also support the current messages=expected_messages case.

    May 6th, 2008 @ 12:08 AM

  4. Nathan Wilmes
    Nathan Wilmes

    1. assert_not_valid opposes assert_valid more closely.. we were attempting to mimic the naming convention of assert_not_equal/assert_equal.

    2. I've thought about it, but as you said, it's easiest to support either partial or exact matches, not both. The exact match format has been more rewarding for us at Pivotal.. when unrelated validations muck up the works, we typically want to know about them so that we can start adjusting the validations accordingly.

    May 6th, 2008 @ 12:20 AM

  5. Tarmo Tänav
    Tarmo Tänav

    1. I agree

    2. I agree, though I still think that if there is a all-in-one assertion helper for the "test more than needed, just in case" people there should be an equivalent for those who want to test one thing and one thing only.

    Anyway, about the not passing any messages, are you going to add this to the patch? If so, it's a +1 from me.

    (btw. besides not running the messages assertion if the messages parameter is blank the assertion failure message for the validation check would also need a different text as it can't really report what errors it was expecting if the errors are not specified)

    Thank you,

    May 6th, 2008 @ 12:52 AM

  6. Nathan Wilmes
    Nathan Wilmes

    Ok, here's the updated patch that makes additional parameters optional.

    May 6th, 2008 @ 05:38 PM

  7. Tarmo Tänav
    Tarmo Tänav

    Thank you. Is there a good reason for not squishing the commits in the patch?

    May 6th, 2008 @ 05:48 PM

  8. Nathan Wilmes
    Nathan Wilmes

    No good reason... I've just started using git, so I'm still working out how to do some of these kinds of operations.

    May 6th, 2008 @ 06:23 PM

  9. josh
    josh
    • State changed from new to invalid
    • Assigned user set to josh

    I think we saw assert_invalid before, and it was turned down.

    We already trimmed out a bunch of superfluous assert_* response helpers. When there is a helper for just about every case, it becomes so hard to remember to use them.

    assert_equal company.errors.on(:email), "can't be blank"
    

    May 8th, 2008 @ 06:39 AM