Jens Krämer

Integration test your file uploads

 |  plugins, ruby, rails

Surprisingly (given Rails’ generally good testing support), it is not possible to do multipart post requests in integration tests.

At least until now ;-) What is needed to allow for multipart post requests is a method that builds the correctly encoded body from the given parameters, including correct MIME boundaries and headers for the different parts. For easy reuse I put this into a small plugin.

##Installation

script/plugin install svn://projects.jkraemer.net/plugins/multipart_integration_test

##Use

Wherever you create your testing session, just extend it with the MultipartPost module: open_session do |s| s.extend(MultipartPost) upload = fixture_file_upload '/files/dummy.jpg', 'image/jpeg' s.multipart_post '/files/upload', :uploaded_data => upload # assertions go here... end Most often you’ll have a custom method for creating new sessions as a part of your DSL, so you could well extend the session object once in this place.

##To do

I tried to generally include the module into the Session class in init.rb, however this didn’t work out. If you find out how to do this, please drop me a line.