In this article, we will review how to use Ruby RSpec for testing REST APIs like GroupDocs. RSpec is a BDD (behavior driven development) framework. You can find more info about RSpec on its official site. There are a lot of different GroupDocs APIs that you can use. In this article we will test one of the User APIs: “Get user profile”.
What You Need
You need Ruby, Gem, Bundler, and RSpec (gem install rspec) installed on you computer. You also need a GroupDocs account and private and client keys. Find out how to get GroupDocs API keys.
Getting Started
First of all, create a folder for future tests (for example groupdocs_tests) and create file with the name Gemfile with the following content:
source 'https://rubygems.org'
gem 'rake'
gem 'rspec'
gem 'ci\_reporter'
gem 'groupdocs'
Here we define all gems that we will need for API testing. The first line defines the source for gems. Then, we list the gems we’re using:
- gem ‘rake’ - a build tool for Ruby.
- gem ‘rspec’ - a BDD testing framework.
- gem ‘ci_reporter’ - an add-on to Test::Unit and RSpec that allows you to generate XML reports of your test and/or spec runs.
- gem ‘groupdocs’ - the GroupDocs Ruby SDK.
After adding the Gemfile file, you need to use the bundle and install all required gems. We will install gems locally by commands in a terminal. Make sure that you are in the groupdocs_tests directory:
bundle install --path vendor/bundle
Creating Test Folders
Now we will create additional folders for our test:
- In the test’s root directory (groupdocs_tests), create a spec folder. All Ruby project tests should be in the spec folder.
- In the spec folder, create a user folder. As we will test the User API we will put all our User test in this folder
Writing the Code
Now let’s write some Ruby code.
- Create a spec_helper.rb file in the spec/ folder. We will use this file to configure the GroupDocs SDK and for some helpers.
- Create a get_user_profile_spec.rb file in the user/ folder. Specs files name should ends with the “_spec” suffix. “get_user_profile” is the name of the API that we will test.
/groupdocs_tests/spec/spec_helper.rb content
require 'groupdocs'
RSpec.configure do |spec|
# configure API access
GroupDocs.configure do |groupdocs|
groupdocs.api\_server = "https://api.groupdocs.com"
groupdocs.api\_version = "2.0"
groupdocs.client\_id = '' #your Client ID here
groupdocs.private\_key = '' #your Private Key here
end
# share API tests methods
spec.shared\_context :api\_tests do
let(:response) do |spec|
response = request.execute!
end
end
end
/groupdocs_tests/spec/user/get_user_profile_spec.rb content
require 'spec\_helper'
describe 'User' do
describe 'GetUserProfile' do
include\_context :api\_tests
let(:request) do
GroupDocs::Api::Request.new method: 'GET',
path: '/mgmt/{{client\_id}}/profile'
end
it 'returns not empty user hash' do
response\[:user\].should\_not be\_empty
end
it 'returns user identifier' do
response\[:user\]\[:id\].should be\_a(Fixnum)
end
it 'returns user primary email' do
response\[:user\]\[:primary\_email\].should == "" # your primary email here
end
it 'returns user private key' do
response\[:user\]\[:pkey\].should == "" # your private key here
end
end
end
Enter your keys and email where it needed. That’s all you need to do.
Running the Test
Make sure that your test work fine, in terminal:
rspec spec/user/get\_user\_profile\_spec.rb
If everything is fine, you will see something like this:
Coming Up
In the next part, we will see how to test an API with the “POST” and “PUT” methods:
- What is expectation (should and should_not) and how do we use them?
- What is matchers and how do we use them?