Rspec测试CSV文件下载

时间:2013-12-17 17:09:16

标签: ruby-on-rails ruby rspec

我想确保我的CSV下载包含正确的列。当我使用RSpec测试CSV下载时,我无法访问文件内容。如何访问CSV文件的内容?

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

测试结果:

# This is the output in the terminal
# ""
# {"Content-Type"=>"text/csv; charset=utf-8", "Content-Disposition"=>"attachment; filename=\"applications-2013-12-17.csv\""}

1 个答案:

答案 0 :(得分:7)

在您描述的阻止电话render_views中,如:

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

调用render_views指示RSpec在控制器规范中呈现视图内容。默认情况下这是关闭的,因为当您运行控制器规范时,通常不关心视图内容,这会使您的测试运行得更快。

您可以查看最新Rails版本here的官方文档。