问题
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?
回答1:
The DRY principle applies to test code just as much as it applies to application code.
Having one method to generate all of those tests should make it easier to verify that the test is correct in the first place.
To answer the comment (note: I haven't written Rails test code in a while, so it probably isn't 100% correct). Everything between %| | is one big string:
MyControllerTest
[:index, :show, :new, :create, :edit, :update, :destroy].each do |action|
class_eval do %|
test "#{action} requires before filter" do
#test #{action} code here
end
|
end
end
回答2:
In general, write your tests individually. However, if you have a bunch of identical tests, I see nothing wrong with generating each one from an each block such as you have.
Bonus tip: use RSpec, not Test::Unit. Besides being generally nicer, it makes what you're doing a whole lot easier.
来源:https://stackoverflow.com/questions/1137589/is-it-possible-and-or-advisable-to-generate-tests-dynamically-in-rails