TEST for REST!

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

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

> 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

The 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

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?

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!

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.

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?

I've answered my own question:

get "/action.xml", :content_type => "application/xml", :authorization
=>
ActionController::HttpAuthentication::Basic.encode_credentials('user',
'password')

Cheers, Olly