What's the use of writing tests matching configuration-like code line by line?
- by Pascal Van Hecke
Hi,
I have been wondering about the usefulness of writing tests that match code one-by-one.
Just an example: in Rails, you can define 7 restful routes in one line in routes.rb using:
resources :products
BDD/TDD proscribes you test first and then write code. In order to test the full effect of this line, devs come up with macros e.g. for shoulda: http://kconrails.com/2010/01/27/route-testing-with-shoulda-in-ruby-on-rails/
class RoutingTest < ActionController::TestCase
# simple
should_map_resources :products
end
I'm not trying to pick on the guy that wrote the macros, this is just an example of a pattern that I see all over Rails.
I'm just wondering what the use of it is... in the end you're just duplicating code and the only thing you test is that Rails works. You could as well write a tool that transforms your test macros into actual code...
When I ask around, people answer me that:
"the tests should document your code, so yes it makes sense to write them, even if it's just one line corresponding to one line"
What are your thoughts?