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
First of all, create a folder for future tests (for example groupdocs_tests) and create file with the name Gemfile
with the following content:
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.
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
# share API tests methods
spec.shared_context :api_tests do
let(:response) do |spec|
response = request.execute!
describe 'User' do
describe 'GetUserProfile' do
GroupDocs::Api::Request.new method: 'GET',
it 'returns not empty user hash' do
it 'returns user identifier' do
it 'returns user primary email' do
response[:user][:primary_email].should == "" # your primary email here
it 'returns user private key' do
response[:user][:pkey].should == "" # your private key here
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:
If everything is fine, you will see something like this:
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?