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 prescribes 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?
It's worth looking under the hood to see what the shoulda macro actually does. It checks each route generated by
resources :products
, i.e. it doesn't simply verify that theresources
statement exists in the routes file. So this is not really an instance of testing Rails code that has already been tested.