Ever send down a CSV from a controller like this:
def index
@users = User.all
respond_to do |format|
format.csv { send_data @users.to_csv, filename: "users-#{Date.today}.csv" }
end
end
And then wonder how you can test the contents as well as the filename, and the fact that it's supposed to be a download?
To check the filename, look at response.headers['Content-Disposition']
.
expect(response.headers['Content-Disposition'])
.to eq 'attachment; filename=users-2020-10-23.csv'
To check the contents, look at response.body
:
expect(response.body).to eq <<~CSV
First Name,Last Name
Andrew,Smith
CSV