Hi,
I found this test helper on the Rails wiki, works like a charm.
# returns an uploaded screenshot you can use to test file uploads in
your controllers
def uploaded_screenshot
uploaded_file("#{File.expand_path(RAILS_ROOT)}/test/fixtures/
samplescreenshot.jpg", "image/jpeg")
end
# returns an uploaded movie you can use to test file uploads in your
controllers
def uploaded_movie
uploaded_file("#{File.expand_path(RAILS_ROOT)}/test/fixtures/
samplemovie.flv", "video/flv")
end
# get us an object that represents an uploaded file
def uploaded_file(path, content_type="application/octet-stream",
filename=nil)
filename ||= File.basename(path)
t = Tempfile.new(filename)
FileUtils.copy_file(path, t.path)
(class << t; self; end;).class_eval do
alias local_path path
define_method(:original_filename) { filename }
define_method(:content_type) { content_type }
end
return t
end
put that in test_helper.rb
Then in your controller test, put this:
# update a fragment
# uses test helper uploaded_screenshot() to simulate a file upload
def test_should_update_fragment
put :update, :id => 1, :fragment => { :title => 'new f' },
:screenshot => { :uploaded_data
=> uploaded_screenshot }
assert_redirected_to fragment_path(assigns(:fragment)),
assigns(:fragment).errors.inspect
end