RSPEC Controller Test Basics

Source: Internet
Author: User

(1) factorygirl.attributes_for (: contact) generate hash

(2) assigns (: Variable_name) method, which can be understood as assigning a @variable_name to a local variable after accepting action processing

(3) key points of the controller test:

(1) The action of the test Get Request (index, show, New, edit): Whether the return value is what we expect (expect (assigns (: Variable_name)). To EQ variable, When the return value is an array and does not require an array order with Match_array (array), the order is ordered with the EQ; expected return is a new object can be used Be_a_new (Class)); The rendered page is not what we expect (expect (response). To Render_template:action); Example:

 

(2) Test POST request (create): and get request similar idea, only the parameters need hash, need to use Attributes_for (), also need to be divided into the parameters of the legal and not legal two cases, create successfully expected data bar number changes, example:

expect{post:create, contact:attributes_for (: Contact)}.to Change (Contact,: count). by (1)

The HTTP request is placed in the expect block for deferred execution, and the detected value is executed two times before and after the request is executed to verify the change

(3) Test patch request (UPDATE): The parameter is divided into legal and illegal cases, respectively verify whether the return data, and whether the success of the modification, as well as the rendering template;

(4) Test delete request (destroy): Change (Class,: Count). by ( -1), redirect_to

(5) Test non-crud action: There is a return value to verify the return value, or verify the change in the number of data bars; Validate Render page

(6) test the controller that does not output HTML: You can verify the Content-type of the response header (expect (response.headers[' Content-type '). To Have_content ' Text/csv ') ; You can verify that the body contains a specific content (expect (response.body). to Have_content "), Have_content is a method provided by Capybara, or you can use the match provided by Rails-rspec ( ) parameter is a regular expression


RSPEC Controller Test Basics

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.