Ruby configures rspec and RestClient to detect the server, rspecrestclient

Source: Internet
Author: User

Ruby configures rspec and RestClient to detect the server, rspecrestclient

If you manually test the Restful service, it will be very monotonous. Of course, there are some browser plug-ins that can help you manually test through visual interfaces, such as postman and rest console. However, you need to perform a large number of tests manually each time the system version is updated, obviously, this method is not very practical. Ruby has many excellent gem packages that you can use to complete this boring job. Among them, RestClient is one of my favorite ones. With the rspec framework of ruby, you can use RestClient to write very powerful test scripts. Suppose Github wants you to test their Restful API. The first thing you may want to do is ensure that the endpoint returns your expected response code. Before you start, make sure that you have installed the corresponding gems. The most convenient method is to use bundler for installation:

 source "https://rubygems.org" gem 'rest-client' gem 'rspec' gem 'json_expressions'

Go to the cmd (windows, linux, or mac environment) to go to the same-layer directory of the gemfile you created and run 'bundle'

rafs-computer:rafael$ bundleUsing diff-lcs 1.2.5Using json_expressions 0.8.3Using mime-types 2.3Using netrc 0.7.7Using rest-client 1.7.2Using rspec-support 3.1.1Using rspec-core 3.1.4Using rspec-expectations 3.1.2Using rspec-mocks 3.1.2Using rspec 3.1.0Using bundler 1.7.3Your bundle is complete!

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

require 'rspec'require 'rest_client'describe 'GitHub API' do    it 'should return information about a user' do     result = RestClient.get 'https://api.github.com/users/rest-client', :content_type => :json, :accept => :json    expect(result.code).to eq(200)  endend

Run rspec-f doc filename on the command line

Therefore, the return code is correct, but how do we know that the returned json is also correct?

There are several different ways to verify. One way is to parse the json (consisting of key and value) in the body, and then create assertions for each key you want to check. This method can be used, but it requires you to write multiple assertions and is difficult to maintain. Another method is to compare it with a data file in known valid json format. You can use the json_expressions gem package to do this. The following example shows the same spec file. Add a new testcase to verify json data.

First, prepare a users. json file.

{ "login": "rest-client", "id": 2386701, "avatar_url": "https://avatars.githubusercontent.com/u/2386701?v=3", "gravatar_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, "public_gists": 0, "followers": 0, "following": 0, "created_at": "2012-09-20T15:01:43Z", "updated_at": "2015-03-11T19:08:01Z"}

Then write the spec file for the test case.

require 'rspec'require 'rest_client'require 'json_expressions/rspec'describe 'GitHub API' do it 'should return 200 when asking information about a user' do  result = RestClient.get 'https://api.github.com/users/rest-client', :content_type => :json, :accept => :json  expect(result.code).to eq(200) end it 'should return proper data for a user' do  expected_data = JSON.parse(IO.read('users.json'))  result = RestClient.get 'https://api.github.com/users/rest-client', :content_type => :json, :accept => :json  expect(result).to match_json_expression(expected_data)  endend

The users. json file contains a known response. As you may have guessed, some such service return values can change very quickly. For example, "updated_at" is the key that may change frequently in the returned value. If you only want to verify whether the key exists and do not care about its value, you can add the following code to your test case.

It 'should return proper data for a user' do expected_data = JSON. parse (IO. read ('users. json ') # parse users. the data in the json file is used as the expected value result = RestClient. get 'https: // api.github.com/users/rest-client',: content_type =>: json,: accept =>: json # expect CT (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.