Ruby configuration RSpec and restclient to detect server _ruby topics

Source: Internet
Author: User
Tags json response code

If you test the RESTful service by hand, it will be very tedious. Of course, there are some browser plug-ins that can help you manually test by using a visual interface, such as postman, rest console, but every time a system version is updated, you need to perform a lot of tests manually, which is obviously not very practical. Ruby has a lot of excellent gem packages, and you can use them to do the boring work. One of the restclient is one I like better. The RSPEC framework, combined with Ruby, uses Restclient to write very powerful test scripts. If GitHub wants you to test their restful API. The first thing you might want to do is make sure that the endpoint returns your expected response code. Before you begin, you need to make sure that you have installed the appropriate gems. The most convenient way is to use the Bundler installation:

 SOURCE "https://rubygems.org" Gem '

 rest-client ' gem '
 rspec '
 gem ' json_expressions '

In cmd (WINDOWS,LINUX/MAC environment please resolve) into the same level directory where you create gemfile files, run ' bundle '

rafs-computer:rafael$ bundle
using Diff-lcs 1.2.5
using json_expressions 0.8.3
using Mime-types
Using NETRC 0.7.7 using
rest-client 1.7.2 using rspec-support using
Rspec-expectations 3.1.2
using rspec-mocks 3.1.2
using RSpec 3.1.0
Using bundler 1.7.3 Your Bundle
Is complete!

Now let's verify that we get 200 responses from the user terminal:

Require ' RSpec '
require ' rest_client ' describe

' GitHub API ' do
  
  it ' should return information about a user ' do< C4/>result = Restclient.get ' https://api.github.com/users/rest-client ',: Content_Type =>: JSON,: Accept =>: json< C5/>expect (Result.code). to Eq.



Executing rspec-f doc filename at the command line

So the return code is right, but how do we know that the returned JSON is also right?

There are several different ways to verify. One approach is to parse the JSON in the body (made up of Key,value), and then create an assertion for each key you want to check. This approach can be used, but it requires you to write multiple assertions and is more difficult to maintain. Another method is to compare to a data file in a known-valid JSON format. You can use the Json_expressions gem package to do this thing. The following example is the same spec file. A new testcase is used to validate JSON data.

First prepare a Users.json file

{"Login": "Rest-client", "id": 2386701, "Avatar_url": "Https://avatars.githubusercontent.com/u/2386701?v=3", "Gravat ar_id ":" "," url ":" Https://api.github.com/users/rest-client "," Html_url ":" Https://github.com/rest-client "," Followers_url ":" Https://api.github.com/users/rest-client/followers "," Following_url ":" https://api.github.com/ Users/rest-client/following{/other_user} "," Gists_url ":" https://api.github.com/users/rest-client/gists{/gist_id } "," Starred_url ":" Https://api.github.com/users/rest-client/starred{/owner}{/repo} "," Subscriptions_url ":" https ://api.github.com/users/rest-client/subscriptions "," Organizations_url ":" https://api.github.com/users/ Rest-client/orgs "," Repos_url ":" Https://api.github.com/users/rest-client/repos "," Events_url ":" https:// Api.github.com/users/rest-client/events{/privacy} "," Received_events_url ":" https://api.github.com/users/ Rest-client/received_events ", type": "Organization", "Site_admin": false, "name": "Rest-client Team", "Company ": null," blog ":" "," location ": null," email ": null," hireable ": false," bio ": null," Public_repos ": 1," Pu Blic_gists ": 0," followers ": 0," Following ": 0," created_at ":" 2012-09-20t15:01:43z "," Updated_at ":" 2015-03-11t19:08:
 01Z "}

Then write the test case spec file

Require ' rspec ' require ' rest_client ' require ' json_expressions/rspec ' describe ' GitHub ' the


API ' do

 it ' Should return asking information about a user ' does result
  = Restclient.get ' https://api.github.com/users/rest- Client ',: Content_Type =>: JSON,: Accept =>: JSON
  expect (result.code). To EQ end

 it ' should Return proper data for a user ' does
  expected_data = Json.parse (Io.read (' Users.json ')) result
  = Restclient.get ' htt Ps://api.github.com/users/rest-client ',: Content_Type =>: JSON,: Accept =>: JSON
  expect (result). to Match_ Json_expression (expected_data) 
 end



This Users.json file contains a known response. As you may have guessed, some of these service return values can be changed very quickly. For example, "Updated_at" is a key that returns values that may change frequently. If you just want to verify that the key exists and don't care about its value, you can add the following code to your test case.

It ' should return proper the data for a user ' do
  expected_data = Json.parse (Io.read (' Users.json ')) #解析users. JSON file as a pre Value result 
  = Restclient.get ' https://api.github.com/users/rest-client ',: Content_Type =>: JSON,: Accept =>: JSON
  # expect (result). to Match_json_expression (expected_data) 
  expected_data[' updated_at ' = Wildcard_ Matcher End
 

Related Article

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.