Re: No route matches error using RSpec even though route DOES match

Phoenix Rising
Thu, 07 Apr 2011 11:15:35 -0000

All right, I think I figured part of this out, but it''s
really ...sloppy.

So apparently, Rails routes don''t like periods in their params.  The
test data I had created was created using the Faker gem, and the
particular "name" attribute it created had a title of Mrs. in it, and
that period is what threw it off.  If I hard-code it to something
like, "foo", it passes.

So now the problem is, how do I tell the router to ignore periods, and
just pass them as part of the parameter, for that specific route?

On Apr 6, 11:16 pm, Phoenix Rising
<polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> 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 towww.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?