Rails: How to test send_file

Suppose a controller transmits a file using send_file, and you would like an integration or functional test to test it by verifying the contents of the file. You cannot directly access the value of response.body, since it is an instance of Proc.

There are some posts on various lists about this, but no one seems to have a definitive answer.

Copying the Rails test code itself (see actionpack/test/controller/send_file_test.rb), I came to the following solution:


content = "this is what i expect"
get "/file"
assert_response(:success)
assert(@response.body.is_a? Proc)
require 'stringio'
output = StringIO.new
output.binmode
assert_nothing_raised { @response.body.call(@response, output) }
assert_equal(content, output.string)