Integration Testing in Ruby on Rails — How to maintain sessions while testing in Rails

November 2, 2006 at 5:52 pm 2 comments

Well, its a natural feel to get amazed out of every other delighted feature provided by RubyonRails and so appreciating it before actually talking about the feature in every second post. This line is for those people who have published that the worst thing about rails is that every rails programmer always just focus on the appreciation of rails and not on the framework per se. As i think the reason behind his(let say) perception is that he might not have tried rails and probably in all posts he have been through yet is that he would have got jealous out of gaining popularity of ruby on rails over jsp and asp and else, and therefore he might not be reading the whole post due to which he just remained untouched with the real appreciable features.
Anyhow, here is my post on a fantastic rails feature – Integration Testing…
RoR is the only web application framework which provides an inbuilt high level of testing. Out of the whole testing the most interesting real time testing is Integrations Testing where you can synchronize with the sessions too unlike in the Functional Testing.

Where exactly we should use Integration Testing ?
Whenever we need to test a series of functionalities which belongs to more than one controller , we should go for Integration Testing and not the Functional one.

Since the functional and unit testing are controller and model centric respectively, rails automatically creates the related functional and unit tests files. But as integrations testing is not confined in any criteria of a specific controller or model, we have to create the integrations file manually… Well, nothing is headache in rails. Its a simple pre-written script, all you need is to call that script with a name you like for whole story you wish to test in the integration test.

Here is a real example of Integration Test in Ruby on Rails

Considerations for test…
We will simply test

  • signing in
  • posting a new article
  • deleting an article with xml_http_request (ajax post request)

Create the test file by running


ruby script/generate integration_test initial_features

Make sure that now you have the file /test/integration/initial_features_test.rb. Rails automatically appends _test at the end of the file name.

For god sake Lets start the testing now 🙂
Code for the file /test/integration/initial_features_test.rb


require "#{File.dirname(__FILE__)}/../test_helper"

class InitialFeaturesTest < ActionController::IntegrationTest
  fixtures :users, :articles

  def test_initial_features
     user = user_for_test
     user.try_to_signin
     user.signin
     user.post_an_article
     user.delete_an_article_with_xhr
  end

  def user_for_test
    open_session do |user|
      def user.try_to_signin
        assert_nil session[:user] # assert_session_has & _has_no have been deprecated
        get "user/signin"
        assert_response :success
        post "user/signin", :email=>"test failure string", :password=>"test failure string"
        assert_nil session[:user]
      end
      def user.signin
        assert_nil session[:user]
        user = users(:first)
        post "user/signin", :email=>user.email, :password=>user.password
        assert_not_nil session[:user]
        assert_response :redirect
        assert_redirected_to "articles/show"
        # now as the session is set once, we need not to signin again
      end
      def user.post_an_article
        get "articles/show"
        assert_response :success
        assert_template "articles/show"
        user = session[:user]
        articles_count = user.articles.length
        post_via_redirect "article/new", :title=>"Integration Tetsing in Rails", :description=>"another relishing rails feature"
        assert_template "articles/show"
        assert_equal articles_count.next, user.reload.articles.length
      end
      def user.delete_an_article_with_xhr
        user = session[:user]
        articles_count = user.articles.length
        xml_http_request "articles/delete", :id=>articles(:first).id
        assert_equal articles_count-1,user.reload.articles.length
      end
    end
  end

end

Although these are not that high level integration tests that rails can provide but its just an overview on the integration tests. I will explain them soon.

Advertisements

Entry filed under: integration, ror, Ruby on Rails, rubyonrails, tests.

RoR(Ruby on Rails) in India – Ruby on Rails based Indian Company Javascript Validations and Encryptions — how to use javascript encryptions in rails.

2 Comments Add your own

  • 1. Akhil Bansal  |  November 3, 2006 at 5:17 am

    Hi Sur,
    I completely agree with you.

    Reply
  • 2. Ritu  |  November 3, 2006 at 6:54 am

    No doubts integration testing in rails (infact testing in all) is superb and changes the perception of a normal php/perl programmer towards web programming.

    And for all those who are beating in bush to make Rails lovers stop singing the praises 😉 …

    ” David Geary, who has authored books on Java and sits on the technical committee for the latest Java Web programming model, has found that Ruby on Rails is five to 10 times faster than comparable Java frameworks. Like Java did a decade ago, Ruby on Rails is getting developers excited by making them more productive, he said.”….

    Picked these lines from http://news.com.com/Ruby+on+Rails+chases+simplicity+in+programming/2100-1007_3-5920169.html?tag=nefd.lede

    Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Contact

sur.max(at)gmail.com
November 2006
M T W T F S S
« Oct   Dec »
 12345
6789101112
13141516171819
20212223242526
27282930  

Power Shots

Tokyo

Moke Lake

Lower Kentucky Falls (Mapleton, OR)

Glorious Morning

Three of a kind

Peace, quiet and tranquility

Stag Beetle

In het gras/Into the grass

Detached.



More Photos

Subscribe


%d bloggers like this: