Method: Test::Unit::TestCase#upload

Defined in:
lib/test_case.rb

#upload(path, content_type = :guess, type = :tempfile) ⇒ Object

Returns a Tempfile object as it would have been generated on file upload. Use this method to create the parameters when emulating form posts with file fields.

Example:

def test_file_column_post
  entry = { :title => 'foo', :file => upload('/tmp/foo.txt')}
  post :upload, :entry => entry

  # ...
end

Parameters

  • path The path to the file to upload.

  • content_type The MIME type of the file. If it is :guess, the method will try to guess it.



24
25
26
27
28
29
30
31
32
33
# File 'lib/test_case.rb', line 24

def upload(path, content_type=:guess, type=:tempfile)
  if content_type == :guess
    case path
    when /\.jpg$/ then content_type = "image/jpeg"
    when /\.png$/ then content_type = "image/png"
    else content_type = nil
    end
  end
  uploaded_file(path, content_type, File.basename(path), type)
end