Rspecでダウンロードされたかテストする方法


viewに以下のようにsend_fileでファイルをダウンロードした時は、Rspecではcontent_typeを使うと良いらしい。

views
send_file(t.path, type: 'application/zip', dispositon: 'attachment', filename: "hoge.zip")
download_spec.rb
RSpec.describe 'ダウンロードを確認する', type: :request, js: true do
  describe 'ダウンロード' do
    context '正常の場合' do
      it 'ダウンロードできる' do
        visit admin_courses_path
        click_button "ダウンロード"
        expect(page.response_headers['Content-Disposition']).to include("hoge.zip")
        expect(page.response_headers['Content-Type']).to eq("application/zip")
      end
    end
  end
end