简体   繁体   中英

download CSV file and check columns in the RSpec test

I want to make sure my CSV download contains 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?

describe TrackingsController do
  it 'returns a successful 200 response for csv format' do
    signin_user = create(:admin_user)

    sign_in(signin_user)
    get :index, format: :csv

    expect(response).to be_successful
    expect(response.headers['Content-Type']).to eq('text/csv; charset=utf-8')
    expect(response.request.fullpath).to eq('/api/v1/trackings.csv')
  end
end

The above RSpec test was passed but, how I can open the CSV file and check columns?

I'm trying to see response.body but it's give me an empty string

I added render_views it renders the view's in the controller spec. If you don't put render_views, the views won't render, which means the controller is called but after it returns the views are not rendered. Controller tests will run faster, as they won't have to render the view, but you might miss bugs in the view. after that I use CSV.parse(response.body) for parsing our response, then I can check the CSV contents file.

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