File Column Unit Testing
In the Rails app I’m currently working on, I’m using the wonderful file-column plugin and updating one of my model’s position attribute manually using an after_create callback. It’s working out well. How do I know this? I tested it of course!
But, I ran into a little bit of a gotcha in the course of development. In my unit tests for the aforementioned model, I use fixture_file_upload when creating new instances. file-column treats these as it would any other uploaded file, which is exactly what we want for testing, with one exception: it stores the uploaded files in the same directory space regardless of RAILS_ENV. So, if you’ve uploaded some files through your browser as part of development, your unit tests will stomp anything with the same id.
The solution? It’s simple but undocumented, so here goes. file_column takes a couple configuration options. One of these is :root_path. It defaults to File.join(RAILS_ROOT, “public”). So, here’s the code from my model:
file_column :image,
:root_path => ENV["RAILS_ENV"] == "test" ?
File.join(RAILS_ROOT, "public", "test") :
File.join(RAILS_ROOT, "public"),
:magick => {:geometry => "640x480",
:versions => {:thumb => "128x96"}}
This puts our test files in public/test and our development files in public. I suppose I could move this functionality over to environments/test.rb, but it seems to make sense in the model for now. Opinions and dissent welcomed.
Comments(5)




