Friday, August 26, 2011

Inbulit ruby on rails test method

Hello rubies,
      One of best feature which rails provides is inbuilt testing. I like to write the test case to trace behavior of application.
      Below the list of the method which you use during functional and integration tests.

Functional Test

  1. HTML request
      a. Get method
          get :action_name, :parent_params => {:child1 => "arg1"}
      b. Post method
          post :action_name, :parent_params => {:child1 => "arg1", :child2 => "arg2"}

  2. Ajax request
      a. Post method
          xhr :post, :action_name, , :parent_params => {:child1 => "arg1"}


Integration Test

  If you want to pass additional header than use  
  headers = {'HTTP_HOST' => "localhost", 'Content-Type' => 'text/xml'}

  1. HTML request
      a. Get method
          get_via_redirect '/controller_name/action_name', {:arg1 => 'test'}
      b. Post method 
          post_via_redirect '/controller_name/action_name', {:arg1 => 'test'}, headers
      c. Put method
        
          put_via_redirect '/controller_name/action_name', {:arg1 => 'test'}
      d. Delete method
          delete_via_redirect '/controller_name/action_name', {:arg1 => 'test'}
      f. Instead of method*_via_redirect you can use alternate method
        http_method like post, put, get, delete        
        request_via_redirect http_method, '/controller_name/action_name', {:arg1 => 'test'}
    
  2. Ajax request
      a. Post method
          xml_http_request :post, "/controller_name/action_name", {:arg1 => 'test'}, headers

This are the basic methods. If you need detail information on this then refer

http://guides.rubyonrails.org/testing.html

No comments:

Post a Comment