【问题标题】:Rspec test CSV file downloadRspec 测试 CSV 文件下载
【发布时间】:2014-01-05 14:02:35
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    在您的描述块调用render_views,如下所示:

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

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

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

    【讨论】:

      猜你喜欢
      • 2015-06-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多