Rspec:everyday-rspec the real exercise. 7th. Using the request test-Test API

Source: Internet
Author: User

Test Your app's interactions with non-human users, covering external API

7.1request Test vs feature Test

the RSpec , this is specifically aimed at API test is best placed in the spec/requests in the catalog, separate from the functional tests written earlier.

This test is also not used Capy-bara , because it simulates browser interaction, not program interaction.

We're going to use a response from the previous test controller. HTTP verb: Get , Post , Delete and the Patch .

End-Point: Endpoint.

7.2 Test GET request

The point of the request test and the controller test is that it is not limited to the specific controller, any route that you can use for your app

Bin/rails g Rspec:request Projects_api

Create Spec/requests/projects_apis_spec.rb

It "loads a project" douser = Factorygirl.create (: User)factorygirl.create (:p roject, Name: "Sample project")factorygirl.create (:p roject, Name: "Second Sample Project", Owner:user)#发送HTTP GET request,in order to verify the identity,APIrequires a user's e-mail address and authentication token, so we put it in the parameterget Api_projects_path, params:{User_email:user.email,User_token:user.authentication_token      }expect (response). To Have_http_status (: Success)

#调试: See what Response.body is. An array that contains information about the projects of the logged-on user.

[{"id": 2, "name": "Second Sample Project", "description": "A Test Project.", "due_on": "2018-05-26", "Created_at": " 2018-05-19t07:30:14.169z "," Updated_at ":" 2018-05-19t07:30:14.169z "," user_id ": 1}]

Byebugputs "---#{response.body}---"JSON =Json.parse(response.body)puts "---#{json}---"Byebug

#调试: Convert to JSON: change to =

[{"id" =>2, "name" = "Second Sample project", "description" and "A test project.", "due_on" and "2018-05-26", " Created_at "=" 2018-05-19t07:30:14.169z "," Updated_at "and" 2018-05-19t07:30:14.169z "," user_id "=>1}]

expect (json.length). to EQ 1project_id = json[0]["id"]puts "---#{project_id}---"Byebug# Expect JSON data to be one, then get this project's??。 Used to request the API again for more information on this project.GetApi_project_path (project_id), params:{User_email:user.email,User_token:user.authentication_token      }expect (response). To Have_http_status (: Success)JSON =Json.parse(response.body)expect (json[' name ']). To eq "Second Sample Project"

#最后 to parse the JSON data returned by the second request. See if it's match.

End

7.3 Testing the POST request

Rspec:everyday-rspec the real exercise. 7th. Using the request test-Test API

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.