Rspec test CSV file download

rspec, ruby, ruby-on-rails

Solution

At your describe block call `render_views` as in:

describe Admin::ApplicationsController do
  render_views
  ... # all the other code
end

Calling `render_views` instructs RSpec to render the view contents inside a controller spec. This is turned off by default because when you're running controller specs you usually don't care about the view contents and this makes your tests run faster.

You can see the official documentation for the latest Rails version here.

Problem

I want to make sure my CSV download contain the correct columns. When I test a CSV download with RSpec I cannot access the file contents. How do I access the contents of the CSV file? ``` require 'spec_helper' include Devise::TestHelpers describe Admin::ApplicationsController do before(:each) do @application = FactoryGirl.create :application @user = FactoryGirl.create( :admin_user ) sign_in @user end it "downloads a csv" it "gives us only the columns we want" do get :index, format: :csv p response.body p response.headers end end ``` The output of the test: ``` # This is the output in the terminal # "" # {"Content-Type"=>"text/csv; charset=utf-8", "Content-Disposition"=>"attachment; filename=\"applications-2013-12-17.csv\""} ```

Original source