Subscribe to RSS Feed

In my current Ruby on Rails project, I’m learning about Notahat’s machinist testing framework to build object graphs on which to write unit tests. Its a nice improvement on fixtures; using an object-orientated, programmatic approach to creating test data.

In the pursuit of 100% code coverage, I hit on a problem when trying to test UploadColumn, which was being used for handling images on a Product class. Normally in a Test::Unit class, I could invoke the fixture_file_upload method to simulate the multi-part form upload of an image, but in machinist I couldn’t do that. After finding next to nothing in the way of documentation for testing Upload column, I did some rummaging around in the gem and found the solution.

At the top of your blueprints file, include UploadColumn:

include UploadColumn

Then, in your blueprint, you can use the UploadedImage class to represent your uploaded image:

Product.blueprint do
 title
 description
 image { UploadedFile.upload(File.new(RAILS_ROOT + '/test/fixtures/label.png')) }
end

I hope that helps somebody…

Tags: , , ,

Leave a Reply