Why is it that I can’t do “get posts_url” in my post functional
testing? posts_url correctly evaluates to “http://test.host/
posts” (*), but any attempt to get this url fails with an
UnknownAction. Similarly, “get ‘/posts’”, “get ‘/posts/index’” etc
all fail. “get ‘index’” works fine.
Which may sound trivial, but it seems silly to not be able to use
“get new_post_url” with your restful controllers.
The main reason it’s falling over seems to be that the TestProcess
module is very action-based - it doesn’t expect to see the full
“http://test.host/posts/index” url. I’ve hacked in a workaround that
strips out everything but the action:
module ActionController::TestProcess
alias_method :orig_process, :process
def process(action, parameters = nil, session = nil, flash = nil)
#Strip out occurrences of (eg) ‘http://test.host/posts’, leaving
us with just the action.
nameless_action = action.to_s.gsub(/http://#
{@request.host_with_port}/#{@controller.class.controller_path}/?/, ‘’)
nameless_action = ‘index’ if nameless_action.blank?
orig_process(nameless_action, parameters, session, flash)
end
end
- but it’s pretty ugly, and I suspect it’ll break on all but the
simplest routes. Can anyone else do better?
Jon
-
- once you’ve done the “get :index” hack - http://rails.techno-
weenie.net/tip/2006/7/22/test-named-routes-or-url_for-link_to)
- once you’ve done the “get :index” hack - http://rails.techno-