ruby-on-rails – 如何在水豚和恶作剧中测试CSV下载?
发布时间:2020-12-16 21:02:02 所属栏目:百科 来源:网络整理
导读:我需要能够测试我的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['Conten
我需要能够测试我的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). 解决方法
我现在正在经历同样的问题.这可能对你有帮助. =>
http://sponsorpay.github.io/blog/2012/11/29/capybara-poltergeist-and-csv-downloads/
这也可能是相关的. => Downloading file to specific folder using Capybara and Poltergeist driver (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |