I need to check the contents of a CSV file returned by a Rails application.
In my controller, the code looks like this:
respond_to do |format|
format.html
format.js
format.csv do
if current_user.has_rights?
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename=info.csv'
send_data generate_csv_file
else
send_data "Access denied"
end
end
end
And this code works - if I find this URL with the appropriate rights, then the CSV file is downloaded. However, I cannot find any suitable test that will work with Poltergeist and Capybara.
If, after answering this question, I do the following :
describe DashboardsController do
context "when format is csv" do
render_views
let(:csv_string) { get_csv_headers }
let(:csv_options) { {filename: "report.csv", disposition: 'attachment', type: 'text/csv; charset=utf-8; header=present'} }
let (:csv_user) {FactoryGirl.create(:csv_user)}
before do
sign_in csv_user
end
it "should return a csv attachment" do
get :index, format: :csv
puts response.headers
puts response.body
end
end
end
The headline that reported through it:
{"Location"=>"http://test.host/", "Content-Type"=>"text/html; charset=utf-8"}
<html><body>You are being <a href="http://test.host/">redirected</a>.</body></html>
which is clearly wrong. What can I do to get an answer for the csv csv format in the context of the test? (note that I have included render_views, as has been suggested in this issue ).