Forum: Ruby on Rails auto #valid? good or bad idea for tests?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
032fdb4cd4a3c65ebb77846dfa724679?d=identicon&s=25 Joe Noon (joenoon)
on 2007-06-20 10:45
(Received via mailing list)
I've recently added this to the bottom of my test_helper.rb (it should
really be in its own file, but first I want to get feedback):

module ActiveRecord
  class Errors
      alias_method :old_on, :on
      def on(attribute)
        self.instance_variable_get("@base").send :valid?

This alows me to avoid calling #valid? manually in every test where I
am checking for a validation error:

topic =
assert_equal topic.errors.on(:title), "must be set"

Instead of:

topic =
assert_equal topic.errors.on(:title), "must be set"

I know that doesnt look like a huge deal, but when you have many tests
which need to do something like this all the #valid? calls get ugly
and repetetive.  So, I'm looking for a reason anyone may have as to
why this would be a bad idea.  Im hoping no one can come up with
reasons why it would be a bad idea, but if there are some I'd rather
find out now.

This topic is locked and can not be replied to.