加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:37:52 所属栏目:百科 来源:网络整理
导读:我想确保我的CSV下载包含正确的列.当我使用RSpec测试CSV下载时,无法访问文件内容.如何访问CSV文件的内容? require 'spec_helper'include Devise::TestHelpersdescribe Admin::ApplicationsController do before(:each) do @application = FactoryGirl.creat
我想确保我的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""}

解决方法

在你的描述块调用render_views,如:
describe Admin::ApplicationsController do
  render_views
  ... # all the other code
end

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读