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?
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
> 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
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:
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.