DEV Community

Augusts Bautra
Augusts Bautra

Posted on

How to test `send_data` in request specs

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

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up