ruby-on-rails - Rspec测试CSV文件下载

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

最佳答案

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

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

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

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

关于ruby-on-rails - Rspec测试CSV文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20640421/

相关文章:

ruby-on-rails - 理论 : method_missing

css - Ruby:网页的计算样式

ruby-on-rails - 在 OS X 上调试 Ruby 2.1 的方法

ruby - 什么时候使用 RSpec let()?

ruby-on-rails - Capybara 无法找到带有文本的隐藏元素

ruby-on-rails - 除了应用程序之外,如何在单独的不同 docker 容器中运行 sidekiq

ruby-on-rails - 使用 Nokogiri 和正则表达式解析 Ruby XML 文档中的编码标签

ruby - 如何在 Ruby 中的特定文件夹中创建文件

ruby-on-rails - 使用 Ruby on Rails 和 RSpec 为 helper 编写规范

javascript - Rails 'nil' 不是与 ActiveModel 兼容的对象。它必须实现 :to_partial_path. ajax