我有一個請求規範,試圖在我的Rails 3.1中測試文件下載功能。該規範(部分)如下:如何使用Rails,Paperclip和RSpec請求規範測試文件下載?
get document_path(Document.first)
logger(response.body)
response.should be_success
它失敗:
Failure/Error: response.should be_success
expected success? to return true, got false
但是,如果我在瀏覽器中測試下載,它正確地下載文件。
這裏是在控制器中的作用:
def show
send_file @document.file.path, :filename => @document.file_file_name,
:content_type => @document.file_content_type
end
我的記錄器提供了有關回應此信息:
<html><body>You are being <a href="http://www.example.com/">redirected</a>.</body></html>
我怎樣才能得到這個測試通過?
更新:
正如一些人士指出,我before_filters的人做重定向。原因是我使用Capybara登錄測試,但沒有使用它的方法來瀏覽網站。這是什麼工作(部分):
click_link 'Libraries'
click_link 'Drawings'
click_link 'GS2 Drawing'
page.response.should be_success #this still fails
但現在我想不出一種方法來測試實際的響應是成功的。我在這裏做錯了什麼。
聲音對我喜歡之前的過濾器(例如,檢查用戶登錄的過濾器)在動作運行之前重定向。 – 2012-01-05 19:29:01
你真的很棒。不過,我仍然無法測試迴應。 – croceldon 2012-01-05 19:59:38
@croceldon:讓我知道下面的登錄方法(在AuthenticationHelpers中)是否有所作爲 - 我很想知道它是否是同樣的問題。 – 2012-01-11 19:34:41