(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