![](/img/trans.png)
[英]Rails - Functional testing of redirect_to(request.referer)
[英]Rails using request.referer in testing
我有一個輔助方法,其中包含以下代碼。 它是AJAX購物車的一部分,顯示在網站的多個頁面上
module CartsHelper
def switch_buttons
unless URI(request.referer).path==new_order_path && !current_page?(store_path) \
|| current_page?(new_order_path)
@checkout = true
else
@checkout = false
end
end
end
這是購物車局部視圖
<h2> Your Cart</h2>
<table>
<%= render(cart.line_items)%>
<tr class ="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price)%></td>
</tr>
</table>
<% if switch_buttons %>
<%= button_to 'Checkout', new_order_path, method: :get %>
<%= button_to 'Empty cart', cart, method: :delete,
confirm: 'Are you sure?', remote: true %>
<% else %>
<%= button_to 'Cancel Order', store_path, method: :get %>
<% end %>
在我的所有測試中,URI(request.referer).path給出了錯誤的參數(預期的URI對象或URI字符串)錯誤。 它可以在實際的瀏覽器中運行。 我猜這是因為測試沒有真正通過URL,所以request.referer為nil? 有什么方法可以設置測試以通過此代碼?
兩件事情:
首先,這回答了您的主要問題:
其次,給定request.referer並不是給定的。 當您從上一頁導航時,大多數瀏覽器都提供標題。 多數情況下您手動輸入網址時不會。 不能假定HTTP客戶端總體上這樣做,並且您必須准備從該屬性中獲取nil。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.