If you are a Software Test Engineer or Quality Control Engineer and you want to automate your API call tests, then you should try RSpec (Ruby’s testing framework). I didn’t exactly chose it (the QC team was already using it), but I tend to believe that I would have picked it in the future for my own tests because when it comes to the installation of the program, the process is not that complicated at all.

Actually, let me show you how little you need to do in order to start writing your own tests:

gem install rspec
rspec --init

Yep, that’s all. Now you can automate your tests and run them with the following command:

rspec your_test_suite.rb

I will now show you some common error messages that I’ve encountered, so that you can avoid them during your work. These errors are caused by very small mistakes, but usually in the rush of delivering quality we skip some things or words.

 

1. syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)

Let’s take a look at the following examples:

describe 'Test Suite' 
   it 'Validate successful response' do
      response = RestClient.get('www.intelligentbee.com')
      expect(response.code).to eq(200)
   end
end
describe 'Test Suite' do
   it 'Validate successful response' 
      response = RestClient.get('www.intelligentbee.com')
      expect(response.code).to eq(200)
   end
end

So, if you get the above error, you most probably forgot to put a ‘do’ after ‘describe’ or ‘it’ methods.

 

2. syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)

I will use the same example again:

describe 'Test Suite' do
   it 'Validate successful response' do
      response = RestClient.get('www.intelligentbee.com')
      expect(response.code).to eq(200)
   
end

What is wrong with this? Well, I missed an ‘end’. I’ll take it you can figure out by yourself where it should be placed.

 

3. JSON::ParserError: 757: unexpected token

Take a look:

describe 'Test Suite' do
   it 'Validate successful response' do
      response = RestClient.get('www.intelligentbee.com')
      parsed_response = JSON.parse(response)
      expect(parsed_response['message']).to eq "Some message"
   end
end

Supposedly, sometimes you will get as an answer a JSON and you will want to parse it for better tests. You will get the above error if the answer is not a JSON and the parser can’t find there what it expects.

I hope you will find this short guide useful, I plan to continue writing about common errors that we may encounter while using RSpec.