Hello everyone! I''m writing tests for my REST API. Now the exposed resources regarding the REST are definately accessible through thier URLs like for the account resource, I''d do this: # Trying to emulate GET /accounts.json get ''/accounts'', {:format => ''JSON''}, {:user => User.find(1) } But the above call fails and gives error that no action responds to ''/ accounts''. Even if I provide the fully qualified URL as ''http:// localhost:3000/accounts'' even then this fails. Because the account is exposed as a resource in the config/routes.rb, we''ve a method accounts_path or accounts_url but both the methods are not accessible in the classes derived from Test::Unit::TestCase. What should I do in this regard? Regards, Mohsin --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 13 Feb 2008, at 07:53, MohsinHijazee wrote:> > Hello everyone! > I''m writing tests for my REST API. Now the exposed resources > regarding the REST are definately accessible through thier URLs like > for the account resource, I''d do this: > > # Trying to emulate GET /accounts.json > get ''/accounts'', {:format => ''JSON''}, {:user => User.find(1) } > > But the above call fails and gives error that no action responds to ''/ > accounts''. Even if I provide the fully qualified URL as ''http:// > localhost:3000/accounts'' even then this fails. > > Because the account is exposed as a resource in the config/routes.rb, > we''ve a method accounts_path or accounts_url but both the methods are > not accessible in the classes derived from Test::Unit::TestCase. What > should I do in this regard? >Unit tests are not supposed to be testing at that level. You might want a functional test (but that doesn''t involve any sort of routing etc...) or integration tests Fred> > Regards, > Mohsin > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 13, 12:55 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 13 Feb 2008, at 07:53, MohsinHijazee wrote: > > > > > > > Hello everyone! > > I''m writing tests for my REST API. Now the exposed resources > > regarding the REST are definately accessible through thier URLs like > > for the account resource, I''d do this: > > > # Trying to emulate GET /accounts.json > > get ''/accounts'', {:format => ''JSON''}, {:user => User.find(1) } > > > But the above call fails and gives error that no action responds to ''/ > > accounts''. Even if I provide the fully qualified URL as ''http:// > > localhost:3000/accounts'' even then this fails. > > > Because the account is exposed as a resource in the config/routes.rb, > > we''ve a method accounts_path or accounts_url but both the methods are > > not accessible in the classes derived from Test::Unit::TestCase. What > > should I do in this regard? > > Unit tests are not supposed to be testing at that level. You might > want a functional test (but that doesn''t involve any sort of routing > etc...) or integration testsThe functional tests are essentially also decedents of the Test::Unit::TestCase as well just only being in a different directory (that is, test/functional). Integration testing is about the flow of control among the components. I''ve placed my REST tests under the test/functional/restapi with the following convention: resourcename_resource_test.rb So for the exposed resource accounts, I''ve the following test test/functional/restapi/accounts_resource_test.rb> > Fred > > > > > Regards, > > Mohsin--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Would you be able to post an example of one of these restapi tests? How are you calling the API in the test and checking the result? On Feb 13, 8:11 am, MohsinHijazee <mohsinhija...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve placed myRESTtests under the test/functional/restapi with the > following convention: > resourcename_resource_test.rb > So for the exposed resource accounts, I''ve the following test > test/functional/restapi/accounts_resource_test.rb--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 4, 4:35 pm, Olly Lylo <o...-2iX6Wuy7WY21Qrn1Bg8BZw@public.gmane.org> wrote:> Would you be able to post an example of one of these restapi tests? > How are you calling the API in the test and checking the result? > > On Feb 13, 8:11 am, MohsinHijazee <mohsinhija...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''ve placed myRESTtests under the test/functional/restapi with the > > following convention: > > resourcename_resource_test.rb > > So for the exposed resource accounts, I''ve the following test > > test/functional/restapi/accounts_resource_test.rbI''ve found a solution. Here is is: Consider you want to test a rest api call like this one: DELETE /users/1/posts/2/comments/3.json this would basically invoke the CommentsController#destroy with following parameters available to this method through params hash: params[:user_id] params[:post_id] params[:id] #ID of the comment to be deleted. So emulate this call in your tests, derive do this in your setup: def setup @controller = CommentsController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end now write a test like this: def test_delete delete {:format => ''json'', :user_id => 1, :post_id => 2, :id => 3}, {} # Any session data you may want in the third hash # And you are ready to roll! assert_response :succuss #More asserstions. end Hope this explains how to write tests for the REST API of your application! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''ve found a solution. Here is is: Consider you want to test a rest api call like this one: DELETE /users/1/posts/2/comments/3.json this would basically invoke the CommentsController#destroy with following parameters available to this method through params hash: params[:user_id] params[:post_id] params[:id] #ID of the comment to be deleted. So emulate this call in your tests, do this in your setup: def setup @controller = CommentsController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end now write a test like this: def test_delete delete :destroy, {:format => ''json'', :user_id => 1, :post_id => 2, :id => 3}, {} # Any session data you may want in the third hash # And you are ready to roll! assert_response :succuss #More asserstions. end Hope this explains how to write tests for the REST API of your application! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
While that tests the controller, I like to test the REST API the same way my client code would use it by sending XML (or JSON, if you like) to the URL using an integration test. class RestXmlTest < ActionController::IntegrationTest def test_create_order post "/orders.xml", "<order><first-name>Michael</first-name></order>", {:content_type => "application/xml"} assert_response 201 end end Clearly, there are all kinds of assertions you can make after this, but this gets you rolling at this level. On Mar 4, 11:47 pm, MohsinHijazee <mohsinhija...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve found a solution. Here is is: > > Consider you want to test a rest api call like this one: > > DELETE /users/1/posts/2/comments/3.json > > this would basically invoke the CommentsController#destroy with > following parameters available to this method through params hash: > params[:user_id] > params[:post_id] > params[:id] #ID of the comment to be deleted. > > So emulate this call in your tests, do this in your setup: > > def setup > @controller = CommentsController.new > @request = ActionController::TestRequest.new > @response = ActionController::TestResponse.new > end > > now write a test like this: > > def test_delete > delete :destroy, {:format => ''json'', :user_id => 1, :post_id => > 2, :id => 3}, > {} # Any session data you may want in the third hash > > # And you are ready to roll! > > assert_response :succuss > #More asserstions. > end > > Hope this explains how to write tests for the REST API of your > application!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for this. My controllers require HTTP Basic authentication. What would be the best way to add support for this to the integration test below? On Mar 5, 7:41 pm, mh <doktah.hah...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> While that tests the controller, I like totesttheRESTAPI the same > way my client code would use it by sending XML (or JSON, if you like) > to the URL using an integrationtest. > > class RestXmlTest < ActionController::IntegrationTest > > def test_create_order > post "/orders.xml", > "<order><first-name>Michael</first-name></order>", > {:content_type => "application/xml"} > assert_response 201 > > end > end > > Clearly, there are all kinds of assertions you can make after this, > but this gets you rolling at this level. > > On Mar 4, 11:47 pm, MohsinHijazee <mohsinhija...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''ve found a solution. Here is is: > > > Consider you want totestarestapi call like this one: > > > DELETE /users/1/posts/2/comments/3.json > > > this would basically invoke the CommentsController#destroy with > > following parameters available to this method through params hash: > > params[:user_id] > > params[:post_id] > > params[:id] #ID of the comment to be deleted. > > > So emulate this call in your tests, do this in your setup: > > > def setup > > @controller = CommentsController.new > > @request = ActionController::TestRequest.new > > @response = ActionController::TestResponse.new > > end > > > now write atestlike this: > > > def test_delete > > delete :destroy, {:format => ''json'', :user_id => 1, :post_id => > > 2, :id => 3}, > > {} # Any session data you may want in the third hash > > > # And you are ready to roll! > > > assert_response :succuss > > #More asserstions. > > end > > > Hope this explains how to write tests for theRESTAPI of your > > application!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''ve answered my own question: get "/action.xml", :content_type => "application/xml", :authorization => ActionController::HttpAuthentication::Basic.encode_credentials(''user'', ''password'') Cheers, Olly On Mar 5, 11:22 pm, Olly Lylo <o...-2iX6Wuy7WY21Qrn1Bg8BZw@public.gmane.org> wrote:> Thanks for this. > > My controllers require HTTP Basic authentication. What would be the > best way to add support for this to the integrationtestbelow? > > On Mar 5, 7:41 pm, mh <doktah.hah...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > While that tests the controller, I like totesttheRESTAPI the same > > way my client code would use it by sending XML (or JSON, if you like) > > to the URL using an integrationtest. > > > class RestXmlTest < ActionController::IntegrationTest > > > def test_create_order > > post "/orders.xml", > > "<order><first-name>Michael</first-name></order>", > > {:content_type => "application/xml"} > > assert_response 201 > > > end > > end > > > Clearly, there are all kinds of assertions you can make after this, > > but this gets you rolling at this level. > > > On Mar 4, 11:47 pm, MohsinHijazee <mohsinhija...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I''ve found a solution. Here is is: > > > > Consider you want totestarestapi call like this one: > > > > DELETE /users/1/posts/2/comments/3.json > > > > this would basically invoke the CommentsController#destroy with > > > following parameters available to this method through params hash: > > > params[:user_id] > > > params[:post_id] > > > params[:id] #ID of the comment to be deleted. > > > > So emulate this call in your tests, do this in your setup: > > > > def setup > > > @controller = CommentsController.new > > > @request = ActionController::TestRequest.new > > > @response = ActionController::TestResponse.new > > > end > > > > now write atestlike this: > > > > def test_delete > > > delete :destroy, {:format => ''json'', :user_id => 1, :post_id => > > > 2, :id => 3}, > > > {} # Any session data you may want in the third hash > > > > # And you are ready to roll! > > > > assert_response :succuss > > > #More asserstions. > > > end > > > > Hope this explains how to write tests for theRESTAPI of your > > > application!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---