1

最近、Cucumber と Subdomain-fu を一緒に使い始めましたが、ひどいことをするようになりました。

次のようなステップ定義があります。

path = grok_path(path)
get path

私の意見では、それは素晴らしく簡単です。しかし、今は次のとおりです。

path = grok_path(path)
get "http://#{subdomain}.example.com#{path}"

これは機能しますが、正確にはきれいではありません。これを行うためのより良い方法は何ですか?

4

2 に答える 2

2

ステップの定義を読みやすくするために、次のように小さなメソッドでそれをラップできます。

def subdomained_path(path, subdomain):
    return "http://#{subdomain}.example.com#{path}"
end

これにより、ステップ定義を次のように整理できます。

path = grok_path(path)
get subdomained_path(path, subdomain)

機能的には、この 2 つは同等です (そして同じようにハッキリしています) が、私が提案した変更により、少なくともコードが少しきれいに見えるようになります。Net::HTTP.get を変更するアクセス権を持っている場合は、実際の "get" メソッドを変更してサブドメイン引数を受け入れ、さらにクリーンにすることができます。

于 2009-07-30T20:15:27.700 に答える
2

これが Cucumber (ここでは Shoulda を使用) にどの程度適用されるかはわかりませんが、他の場所でいくつかの推奨事項を試した後、これは確実に機能するようです:

def in_subdomain(str)
  # test.host == default testing domain, feel free to change to match your usage
  @request.host = "#{str}.test.host"
end

そして、 に電話する前にget、 であることを確認する必要がありますin_subdomain('subdomain-fuuuuuu')。それはURLを適切に設定し、current_subdomain少なくとも(私はすべてをチェックしていません)、サブドメインを指定せずにリダイレクトし、サブドメインにとどまり、他のサブドメイン(または:subdomain => false)へのリダイレクトは依然として正しいredirected_to値を設定します。

たとえば、これらの (高品質であることがわかると思います) テストはパスし、コントローラーの current_subdomain がチェックされます。

should "show on the owner's subdomain" do
  in_subdomain(@user.domain)
  get :show, :id => @user.things.first.id
  assert_response :success
end
should "not show on another users' subdomain" do
  in_subdomain(@random_user.domain)
  get :show, :id => @user.things.first.id
  assert_redirected_to user_url(@random_user, :subdomain => @random_user.domain)
end
should "not show on a non-existent subdomain" do
  in_subdomain("cthulhu-fhtagn")
  get :show, :id => @user.things.first.id
  assert_redirected_to root_url(:subdomain => false)
end
于 2011-09-22T00:54:20.120 に答える