0

次のコードを含むヘルパー メソッドがあります。サイトの複数のページに表示される 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であると推測していますか? このコードを通過できるようにテストを設定する方法はありますか?

4

1 に答える 1

0

2つのこと:

まず、これはあなたの主な質問に答えます:

Rails でテストするときに HTTP_REFERER を設定するにはどうすればよいですか?

第二に、request.referer が設定されるとは限りません。ほとんどのブラウザは、前のページからナビゲートするときにヘッダーを提供します。URL を手動で入力する場合は、ほとんどの場合、そうではありません。HTTP クライアントは全体的にそうすると想定することはできず、その属性から nil を取得する準備をする必要があります。

于 2013-11-08T16:49:19.787 に答える