Rails test "Three" functional testing functional Test

Source: Internet
Author: User
Tags hash

Functional Testing Functional Test

In rails, the test for each action in a single controller is called a functional test. Controller the request to process the Web, render the requested response to the view.

Content included in functional testing

Did the Web request succeed?

Is the user booted into the correct page?

Does the user successfully authenticate?

is the correct content included in the template for the response?

Does the appropriate content appear in the view to the user?

Functional Test decomposition

After you use the Rails G scaffold post or the Rails G Controller command, you will create a Postscontroller corresponding feature test file Test/functional/posts_controller_ Test.rb.

Require ' Test_helper '
     
class Postscontrollertest < actioncontroller::testcase 
  test "should get Index" do
    get:index 
    assert_response:success 
Assert_not_nil assigns (:p osts)
  End 
     

The above test is for Postscontroller's index. Use the HTTP GET method to access this index, and then assert that the response was successful and assign a valid posts variable.

The Get method issues the Web request and loads the results into the response. The Get method has four parameters:

The action you want to test can be either string or symbol. Get "index" or Get:index.

Optional hash format parameters, requested parameters, incoming action parameters. QueryString parameter, or post parameter.

Optional hash format parameter, passing in the session information for the action.

Optional hash format parameters, flash information.

Get (: Show, {' id ' => '}, {' user_id ' => 5})

Call Show this action, the incoming parameter is id=12,session information is user_id=5.

Get (: View, {' id ' => '}, nil, {' message ' => ' booya! '})

Call view this action, the incoming argument is id=12, no session, but includes a flash,flash[:message]= "booya!".

Types of requests that can be used in functional testing

Get

Post

Put

Head

Delete

Four hashes

After a request completes, you have four hashes to use:

Assigns, returns the instance variable used for view in the action.

Cookies, set cookies information.

Flash,flash object.

Session,session information.

In addition to assigns, the other three hashes can access the hash value in two ways, assigns for historical reasons, unlike the other three.

Flash["Gordon"]               Flash[:gordon] 
session["shmession"]          session[:shmession] 
cookies["Are_good_for_" U "]     Cookies[:are_good_for_u] 
      
# because you can ' t use Assigns[:something] for historical reasons: 
assigns[" Something "]          assigns (: something)

Three variables

There are three variables that can be used in functional test:

@controller – Controller to process requests

@request – the request itself

@response – Response to request

This article is from the "It architects in the breakout" blog, please be sure to keep this source http://virusswb.blog.51cto.com/115214/1075409

See more highlights of this column: http://www.bianceng.cn/Programming/project/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.