2014-01-27 69 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

,原因是...鏈接變得無效(像現在這樣),所以像我這樣尋找相同解決方案的人不能再得到解決方案:( –

相關問題