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:

action:

def item_list_as_spreadsheet
filepath = “tmp/items.xls”
workbook = Excel.new(filepath)

# details of writing data to the spreadsheet omitted for space

reasons

workbook.close

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

‘application/vnd.ms-excel’
end

test:

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

Thanks,
Chris

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs