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.

Share this: