Auto #valid? good or bad idea for tests?

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?
old_on(attribute)
end
end
end

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

topic = Topic.new
assert_equal topic.errors.on(:title), “must be set”

Instead of:

topic = Topic.new
topic.valid?
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.

Joe

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs