我需要能够测试我的Rails应用程序返回的CSV文件的内容。
在我的控制器中,代码看起来像这样:
respond_to do |format|
format.html
format.js
format.csv do
if current_user.has_rights?
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename=info.csv'
send_data generate_csv_file
else
send_data "Access denied"
end
end
end这段代码可以工作--如果我以适当的权限访问该URL,则会下载CSV文件。然而,我似乎无法在Poltergeist和Capybara上得到任何合适的测试。
如果我执行以下操作,following the response to this question
describe DashboardsController do
context "when format is csv" do
render_views
let(:csv_string) { get_csv_headers }
let(:csv_options) { {filename: "report.csv", disposition: 'attachment', type: 'text/csv; charset=utf-8; header=present'} }
let (:csv_user) {FactoryGirl.create(:csv_user)}
before do
sign_in csv_user
end
it "should return a csv attachment" do
# @controller.should_receive(:send_data).with("#{csv_string.join(',')}", csv_options).
# and_return { @controller.render nothing: true } # to prevent a 'missing template' error
get :index, format: :csv
puts response.headers
puts response.body
end
end
end通过报告的报头放入:
{"Location"=>"http://test.host/", "Content-Type"=>"text/html; charset=utf-8"}
<html><body>You are being <a href="http://test.host/">redirected</a>.</body></html>这显然是错误的。如何在测试上下文中获取csv格式的响应?(请注意,我已经包含了render_views、as suggested in this question)。
发布于 2014-01-29 09:34:44
我现在也在经历同样的问题。这可能会对你有帮助。=> http://sponsorpay.github.io/blog/2012/11/29/capybara-poltergeist-and-csv-downloads/
这也可能是相关的。=> Downloading file to specific folder using Capybara and Poltergeist driver
https://stackoverflow.com/questions/21389163
复制相似问题