Rspec测试CSV文件下载 [英] Rspec test CSV file download

查看:45
本文介绍了Rspec测试CSV文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make sure my CSV download contain the correct columns. When I test a CSV download with RSpec I cannot access the file contents. How do I access the contents of the CSV file?

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 如下:

At your describe block call render_views as in:

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

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

Calling render_views instructs RSpec to render the view contents inside a controller spec. This is turned off by default because when you're running controller specs you usually don't care about the view contents and this makes your tests run faster.

您可以查看最新版本的 Rails 官方文档 这里.

You can see the official documentation for the latest Rails version here.

这篇关于Rspec测试CSV文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆