Functional tests with actions that send files

I have a number of actions that use the spreadsheet/excel library to
generate an Excel spreadsheet based on model data and send it to the
user using the send_file method.

For a while, I’ve been testing these methods with assert_response
:success, and then checking the response headers to make sure the file
at least looks like what I should be getting. But it occurred to me
recently that I could use the parseexcel library to actually test the
correctness of the contents of the file, if I can access it.

So: is there any way to save the file being sent to a temporary file
from within the functional test? Can the data be accessed using the
response object somehow? I’ve tried poking around at the response
object, but haven’t really gotten anywhere with it.

Here’s the code, if it makes a difference:


def item_list_as_spreadsheet
filepath = “tmp/items.xls”
workbook =

# details of writing data to the spreadsheet omitted for space



send_file filepath, :filename => 'Item List.xls', :type =>



def test_item_list_as_spreadsheet
get :item_list_as_spreadsheet
assert_response :success
assert_equal ‘binary’,
assert_equal ‘application/; charset=utf-8’,
assert_equal ‘attachment; filename=“Item List.xls”’,
assert @response.headers[‘Content-Length’] > 1024