2014-01-27 66 views
4

我需要能够测试我的Rails应用程序返回的CSV文件的内容。如何测试水豚和poltergeist的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 

而这种代码works--如果我访问具有相应权限的那个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格式的响应是csv? (请注意,我已经包含render_views,as suggested in this question)。

回答

2
+3

本网站上的答案应该包括实质内容,而不仅仅是链接(容易变得无效)。如果你想提供链接,你应该在对这个问题的评论中这样做。 – user664833

+1

,原因是...链接变得无效(像现在这样),所以像我这样寻找相同解决方案的人不能再得到解决方案:( –

相关问题