# Auto #valid? good or bad idea for tests?

**URL:** <https://www.ruby-forum.com/t/auto-valid-good-or-bad-idea-for-tests/107305>\
**Category:** Rails\
**Created:** [June 20, 2007, 10:45am UTC](https://www.ruby-forum.com/t/auto-valid-good-or-bad-idea-for-tests/107305 "2007-06-20T10:45:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [June 20, 2007, 10:45am UTC](https://www.ruby-forum.com/t/auto-valid-good-or-bad-idea-for-tests/107305/1 "2007-06-20T10:45:52Z")

</div>

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
