views:

1901

answers:

3

Hi I do not have any front end in my app. I am willing to release just a RESTful API which can be used by different clients. Any pointers how should I proceed towards testing it with cucumber? Every action in the controller generates XML feed only. Any pointers or suggestions?

+1  A: 

Once you've set up your RESTful routes, you should be able to use Webrat to visit the different routes. You can then test that each route returns XML which meets your expectations.

Here's a blog post that describes how to test XML output in RSpec: Testing XML output

Webrat is a headless browser, which simply means that you can simulate a browser without having to open a real browser like FireFox on your development machine. This means that you can simply type something like "visit 'users/'" into your defined steps and simulate a user accessing your application.

Finally the Pragmatic book on RSpec (still in beta), is a great resource on how to use Cucumber, Webrat and RSpec together and drive your application development with BDD.

Praveen Angyan
Thanks :) I do not have any front end my app that means if I do not have a "Delete" button available to destroy a particular resource. Now AFAIK Webrat doesn't let you request a particular url with a specific HTTP verb(GET, POST etc.). How should I proceed towards this problem? I can do this easily with Rspec but it doesn't support "User Stories" any more. It is a different question in it's own. :)
Waseem
You can simulate the GET method simply by visiting a specific URL. In order to simulate the PUT or DELETE verbs, you should be able to use set_hidden_field "_method", "PUT" or set_hidden_field "_method", "DELETE" before visiting the link. I haven't tried this yet though.
Praveen Angyan
The documentation of set_hidden_field for webrat. It says "it verifies that a hidden field exists on the current page and sets the value to given parameter." As I do not have any front end for my app, I do not have any hidden_field or form_field or buttons to interact with. I think I need a way with which I can request different Rest URLs with different HTTP verbs. something like <a href="http://www.bitlasoft.com/tools/rest_client/">this</a> but which can be automated.
Waseem
Opps http://www.bitlasoft.com/tools/rest_client/ here's the link. :)
Waseem
"RESTful routes" is a misnomer. Sounds like RPC.
Wahnfrieden
+2  A: 

I think Webrat is more than what you need. For XML feed testing, you don't need a browser simulator like Webrat which would load pages and analyse all the markup (links, forms etc.) when you really don't have any HTML pages.

You rather need something like Curl (http://curl.haxx.se) or Curb (on rubyforge, which are ruby bindings for Curl), or Patron (on rubyforge).

These libraries can make a request header as per your liking (e.g. setting Content-Type, choosing among GET PUT POST DELETE HEAD etc.) and obtain the response, and probably follow 302 redirections when needed.

The response returned, can be then turned into XML object, and XML parsers available for Ruby can be used to test the output. Also, you can write XMLMapping classes (on rubyforge) to convert XML output into Ruby objects and test their attributes etc. This is much cleaner, IMHO.

Kazim Zaidi
Ok That was really useful. I am using Curb and XMLMapping to test my app. It is working very fine except for some underlying dirty code. But I guess I can have some dirty code in the tests.
Waseem
+2  A: 

The visit function of webrat accepts a http_method as a second parameter. You can also test your api like in the following cucumber rule:

When /^I restfully delete (?:|the )user "([^\"]*)"$/ do |login|
  visit(path_to("user \"#{login}\" page"), :delete)
end
schastar