Test driven development is your friend
Friday 02 November 2007 - 01:45 by sven

Knowing about the great benefits of test driven development, I completely ignored them when I needed a file upload functionality in my new bewiso interface based on ruby on rails. The first test I wrote did not work and after I found out that there was an unpatched bug related to testing file uploads, I removed my test and forgot about the issue.
This turned out to be a big mistake. One month later, I manually tested my file upload to check if my layout would look nice with an embedded picture. The upload failed and ruby complained with this error message:
undefined method `original_filename' for "#<File:0xb71f0cb8>":String
After a bit of reading I found out that the uploaded file is supposed to be available as 'File' object or as 'StringIO' object if the file is bigger than xy.
The string object was not expected. As I did a rails update some days ago and had a new version of actionpack in use, I started looking into lib/action_controller/cgi_ext/cgi_methods.rb and lib/action_controller/cgi_process.rb but could not find any obvious problems. After reading the changelog of actionpack and getting more and more frustrated I found the line that caused all my trouble by accident.
before_filter :validate_utf_params , inside my application controller. This calls a method that goes through my params and makes sure that there are no wired utf8 characters inside the data. It crashed my params object as I did not expect to find something different than a string.
Lesson learned; write tests even for obvious stuff. If I had followed my own procedures with care I would have found this problem immediately after I checked in the utf8 stuff.
ruby on rails test driven development ruby
