これが 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