简体   繁体   中英

Rspec test CSV file download

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\""}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM