Monday, June 29, 2009

Testing File Uploads in Rails

I was frustrated today trying to figure out why the HTTP functions in Integration::Session were converting my UploadStringIO class parameters to String instances using 'inspect'. Turns out for these specific Rails 2.1.2 performance tests you have to use a special class named ActionController::TestUploadFile. Here is the sample documentation:

# Essentially generates a modified Tempfile object similar to the object
# you'd get from the standard library CGI module in a multipart
# request. This means you can use an ActionController::TestUploadedFile
# object in the params of a test request in order to simulate
# a file upload.
# Usage example, within a functional test:
# post :change_avatar, :avatar => + '/files/spongebob.png', 'image/png')
# Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
# post :change_avatar, :avatar => + '/files/spongebob.png', 'image/png', :binary)

No comments: