Rails: Using url_for in a functional test

This post explains how to use url_for inside of a functional test.

http://jotthought.com/articles/2007/05/04/using-url_for-in-ruby-on-rails-tests/

EDIT: Unfortunately, doing it this way seems to break url_for in integration tests, which normally work. As a workaround, I define something like url_for_in_functional_test rather than overriding the original method.

Basically in test_helper.rb, define:


def url_for_in_functional_test(options)
url = ActionController::UrlRewriter.new(@request, nil)
url.rewrite(options)
end

This post also suggests another method:

http://kiribao.blogspot.com/2008/10/undefined-method-urlfor-in-rails.html

Whereby you do something like this:


class GroupControllerTest < Test::Unit::TestCase
# Note: to make sure url_for works in a functional test, include the two files below!
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper

However, this results in example.com URLs instead of the test.host URLs that are produced by functional tests (see http://lists.rubyonrails.org/pipermail/rails/2006-April/030194.html). This can cause some unexpected behavior. For example, I require the full URL in order to make the OAuth signature in my functional tests.