我想确保我的 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/