Waseem
Waseem

Reputation: 8402

Testing RESTful API with Cucumber in a front end less application

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?

Upvotes: 6

Views: 8852

Answers (5)

alvinsj
alvinsj

Reputation: 875

jayzes has shared his cucumber test steps examples using Rack::Test::Methods, JSONpath, Nokogiri etc to write test for json/xml API, you might want to refer and create more for your own steps.

https://github.com/jayzes/cucumber-api-steps

Upvotes: 5

user379217
user379217

Reputation: 81

I was trying to do that and got stuck in a major problem with restful_authentication (using AASM, one of the internal model of restful_auth it seems) and got to that solution to log in:

Given /^I am logged in with a new account$/ do  
  login = "test"
  @current_user = User.new(
     :login => login,
     :password => 'generic',
     :password_confirmation => 'generic',
     :email => "#{login}@example.com",
     :state => "active"
   )
   @current_user.save
   x = User.find_by_login(login)
   x.state = "active"
   x.save!

   visit "/login" 
   fill_in("login", :with => login) 
   fill_in("password", :with => 'generic') 
   click_button
   response.body.should =~ /Logged in successfully/m
end

Modularize it for cleaner testing corpus, this is to demo the concept I found.

Upvotes: 0

schastar
schastar

Reputation: 246

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

Upvotes: 9

Kazim Zaidi
Kazim Zaidi

Reputation: 524

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.

Upvotes: 7

Praveen Angyan
Praveen Angyan

Reputation: 7265

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.

Upvotes: 2

Related Questions