How to test `send_data` in request specs

Augusts Bautra - Aug 22 '23 - - Dev Community

Originally in Andrew Smith's blog. https://www.andrewsouthpaw.com/testing-an-attachment-with-rspec-and-controllers/

def index
    @users = User.all

    respond_to do |format|
      format.csv { send_data @users.to_csv, filename: "users-#{Date.today}.csv" }
    end
  end
Enter fullscreen mode Exit fullscreen mode
expect(response.headers['Content-Disposition'])
  .to eq 'attachment; filename=users-2020-10-23.csv'

expect(response.body).to eq some_path.read
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .