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:
filepath = “tmp/items.xls”
workbook = Excel.new(filepath)
# details of writing data to the spreadsheet omitted for space
workbook.close send_file filepath, :filename => 'Item List.xls', :type =>
assert_equal ‘application/vnd.ms-excel; charset=utf-8’,
assert_equal ‘attachment; filename=“Item List.xls”’,
assert @response.headers[‘Content-Length’] > 1024