One trick I have found very handy in rails application programming is that class_eval
can be used to create methods on the fly. I'm starting to get into testing now and I wonder if a similar idea could be used to generate tests.
For example, I have a before_filter
to require a user to be logged in for all of the actions in a controller. I would like to write tests which ensure that the before_filter
is being applied for all actions. Instead of writing out each test individually I would like to instead generate all of these test automatically.
Is this type of testing advisable, or should I just stick to writing the tests out individually? If it is, how would one go about doing so?
EDIT: This might look something like:
actions = {:index => :get,:show => :get,:edit => :get,:update => :put}
actions.each_pair do |action,type|
class_eval(%Q{def test_user_required_for_#{action}
set_active_user users(:one)
#{type} :#{action}
assert flash[:error]
assert_redirected_to :action => :index
end
})
end
Now that people have verified this could be useful, where would I put a block of code such as this so that it will get executed once and only once to create these tests?