Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Functional Test of Arbitrary or Custom URLs

I have a RESTful resource in my Rails app called "Photo". I'm using Paperclip to serve different "styles" of my photos (for thumbnails and the like), and I'm using a custom route to RESTfully access those styles:

map.connect "photos/:id/style/*style", :controller => "photos", :action => "show"

That's working fine, but I want to write a test to make sure it stays that way.

I already have a functional test to call the Photo controller's show action (generated by scaffold in fact):

test "should show photo" do
  get :show, :id => photos(:one).to_param
  assert_response :success
end

That tests the execution of the action at the URL "/photo/1". Now I want to test the execution of the URL "/photo/1/style/foo". Unfortunately, I can't seem to get ActionController::TestCase to hit that URL; the get method always wants an action/id and won't accept a URL suffix.

How do I go about testing a custom URL?

Update

While checking on @fernyb's answer I found this snippet in the same rdoc

In tests you can simply pass the URL or named route to get or post. def send_to_jail get '/jail' assert_response :success assert_template "jail/front" end

However, when I actually try that, I get an error message:

test "should get photo" do
  get "/photos/1/style/original"
  assert_equal( "image/jpeg", @response.content_type )
end  

ActionController::RoutingError: No route matches {:action=>"/photos/1/style/original", :controller=>"photos"}

I wonder if I'm doing something wrong.

like image 530
Craig Walker Avatar asked Nov 01 '09 03:11

Craig Walker


2 Answers

Use assert_routing to test routes:

assert_routing("/photos/10/style", :controller => "photos", :action => "show", :id => "10", :style => [])

assert_routing("/photos/10/style/cool", :controller => "photos", :action => "show", :id => "10", :style => ["cool"])

assert_routing("/photos/10/style/cool/and/awesome", :controller => "photos", :action => "show", :id => "10", :style => ["cool", "and", "awesome"])

In your integration test you can then do:

test "get photos" do
   get "/photos/10/style/cool"
   assert_response :success
end
like image 98
fernyb Avatar answered Oct 17 '22 02:10

fernyb


From the Rails API documentation:

Route globbing

Specifying *[string] as part of a rule like:

map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?'

will glob all remaining parts of the route that were not recognized earlier. The globbed values are in params[:path] as an array of path segments.

So it looks like you need to pass the :path arguments, to test the action correctly.

like image 36
Matt Haley Avatar answered Oct 17 '22 01:10

Matt Haley