Refactoring the Mess - Hacking Journal [2006/02/08]
Testing is good. Testing is good. Testing is good.
After reviewing the progress I made on the Rails website at work, boss decided to cut a lot of features I had added. Normally this would have been a huge problem as a lot of the code is coupled but with my huge test suite it turned out easy. I planned on spending at least 6 hours to do the changes he wanted but actually it took a little over three.
The reason was I was able to drop the tables, remove the files for that model, and then slowly change the remaining files so they would call the correct methods. Using my tests I was able to do one change at a time, see the effect and then move on. So in under three hours I have the system running leaner and meaner than before. I even had enough time left over to go through and do some minor touchups to the site.
So once again, tests saved my ass. Also now that the “users” are going to use the system I can shorten the feedback cycle so I will not have to make such sweeping changes to the codebase. Keep it simple.
Eric Davis
Comments
-
I'm curious how these tests are outputting, in the page? a log file? How's about a sample?
-
From the terminal. Like how some programs have a `make test`, Rails uses rake. Example of passing tests:
Zen:~/Development/Projects/explainpmt eric$ rake test_units (in /Users/eric/Development/Projects/explainpmt) /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_loader.rb" "test/unit/collection_table_helper_test.rb" "test/unit/iteration_test.rb" "test/unit/milestone_test.rb" "test/unit/project_test.rb" "test/unit/story_test.rb" "test/unit/user_test.rb" Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_loader Started ................................................................................... Finished in 4.709547 seconds. 83 tests, 241 assertions, 0 failures, 0 errors
And lets says I make a tpyo in my codeZen:~/Development/Projects/explainpmt eric$ rake test_units (in /Users/eric/Development/Projects/explainpmt) /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_loader.rb" "test/unit/collection_table_helper_test.rb" "test/unit/iteration_test.rb" "test/unit/milestone_test.rb" "test/unit/project_test.rb" "test/unit/story_test.rb" "test/unit/user_test.rb" Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_loader Started ................................................................................E.. Finished in 4.415019 seconds. 1) Error: test_full_name(UserTest): NoMethodError: undefined method `full_name' for #<user:0x25268f8> /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1501:in `method_missing' ./test/unit/user_test.rb:12:in `test_full_name' 83 tests, 239 assertions, 0 failures, 1 errors rake aborted! Command failed with status (1): [/opt/local/bin/ruby -Ilib:test "/opt/local...] (See full trace by running task with --trace)Notice how it gives you a backtrace to find the error (i.e. this error was called by calling the method 'full_name' when it is really called 'full_names') Also rails have some very verbose logs that one can watch to see exactly what SQL is running and how the app is working. I hope this helps you understand a bit. Eric Davis
