Rspec测试CSV文件下载

我想确保我的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\""} 

在您的describe块中调用render_views如下所示:

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

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

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