[Rails 3] No route matches error using RSpec even though route DOES match

Phoenix Rising
Thu, 07 Apr 2011 05:16:07 -0000

I''ve recently run into a really weird situation that I''m just
not sure
what''s going on here.  I''m trying to write a route that will
allow
searches to be done via GET requests (generally a good idea since it''s
just retrieving information, helps for links and SEO, etc.), and I
have the route properly set up and can access it in development mode
just fine, but when running the controller spec, it triggers an
ActionController::RoutingError stating that there isn''t such a route
that exists.

The route in question:
match ''/search/users/:name'', :to =>
''search#users'', :via => :get

The idea is that if a user goes to www.example.com/search/users/John+Doe
that the search controller passes the params: {:name => "John Doe"}
to
the "users" action.  Indeed, if I run this through my browser, it
works perfectly fine.

The problem is when using RSpec, it doesn''t work at all.  The
corresponding spec for this is:

require ''spec_helper''

describe SearchController do
  before :all do
    Codebase::Application.reload_routes!
    # a futile attempt to force new routes to be ready, still doesn''t
work
  end

  context "a GET to /users with a name" do
    it "should spit out a list of users with that name" do
      get :users, :name => URI.escape(User.make.name)
      response.should be_success
    end
  end
end

Instead of getting a success (which I should given that the action
itself is essentially blank - it exists, just nothing inside it, no
before_filters or anything, and a view for it exists), I get:
ActionController::RoutingError: No route matches {:name => "Foo
%20Bar", :controller => "search", :action =>
"users"}.

Even more insidious, my output from rake routes:
GET    /users/:name(.:format)
{:controller=>"search", :action=>"users"}

This could be understandable if I could duplicate the problem for
other controller tests, but they all work fine!  All my other
controllers are, so far, set up as REST-based resources, and they work
perfectly with standard things like get :index, or put :update, etc.
No routing errors there.

I don''t really see smushing this behavior into a resource set (with
URL-based parameters) as a good idea or a good way to solve this.  I''d
like to know how I can get RSpec to play nice with my routes the way
they''re supposed to.  Any ideas what''s going on here?

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org