Ruby-gems

Start writing tests in Ruby: useful gems

Being a QA engineer is a continuous struggle in finding the right resources in order to get the job done easier and more efficiently. If you are planning to write automated tests in RSpec (Ruby's testing framework), then you should take a look over these gems. Please notice that I am most of the time automating backend tests only, so the libraries I am using are for this purpose mainly.

 

1. RestClient

 

gem install rest-client

If you want to make API calls on RESTful endpoints this should definitely be your choice. This library is easy to use and the response includes code, cookies, headers and body.

Let me show you how to make some calls (GET, PUT, POST, DELETE):
[crayon-5b76d2250cfcc207575594/]
Now you simply use this response for your purposes (response.code, response.body, etc.).

 

2. JSON

 

gem install json

If I told you about RestClient, then the next one should be json. RESTful services will return JSON format in body most of the times so you should parse that response to be easier to work with.
[crayon-5b76d2250cfdb454380208/]
See how simple this is? You only JSON.parse that response and that's all!

Since we are talking about JSON, let me show you how to build one:
[crayon-5b76d2250cfe2473672836/]

 

3. Nokogiri

 

JSON and XML are the most used formats in web development. So you probably guessed that now I will show you some tricks on how to use XML in your awesome tests.

gem install nokogiri

When I have installed this gem on my ubuntu (v14.04) virtual machine, I have had the following error:
[crayon-5b76d2250cfe8581968936/]
But this was quickly fixed after installing ruby-dev and ruby1.9.1-dev:

sudo apt-get install ruby-dev
sudo apt-get install ruby1.9.1-dev

Now let's say you have the following XML:
[crayon-5b76d2250cfed197747599/]
If you want to access the values for username and password, simply do this:
[crayon-5b76d2250cff3687629700/]
Also, you can use xpath instead of css.

Let me show you how to build the previous XML file using Nokogiri:
[crayon-5b76d2250cff9687026969/]

 

4. Sinatra

 

This gem is used to mock endpoints. See more about it here.

 

5. Dotenv

 

gem install dotenv

It is recommended to keep environment variables and stuff like usernames, passwords and URLs in a .env file. In order to load those variables in your tests, you must use this gem.
[crayon-5b76d2250cfff405453147/]
First you load the .env file, then use those variables in your tests.

 

6. Mysql

 

gem install mysql

The name itself says what this is used for. See below how to open a connection to a MySql database and do a simple operation:
[crayon-5b76d2250d005174078657/]
 

I will update this post when I will use some new awesome Ruby library. What gems are you using?


How To Mock Endpoints in Automated Acceptance Tests

The purpose of acceptance testing is to reproduce production environment as much as possible in order to evaluate the software in a 'real-world' situation similar to what the customers will experience and this includes using real endpoints.

But using real endpoints has some disadvantages. In automated acceptance tests, the endpoint will be stressed out and this is not performance testing. Also, you must use production data (even usernames and passwords) and this is not good because you might break something there. The list with the reasons could go on and on and this is why you should mock some endpoints in automated acceptance tests. Also, you should keep your endpoint's URL stored in environment variables to be able to change it easily (production URL or mock URL).

I will show you some basic examples of mocking endpoints written in Ruby, Python and GO.

Mock endpoint in Ruby

I have used Sinatra (DSL for quickly creating web applications in Ruby) which is very easy to use.

Install the gem:

gem install sinatra
[crayon-5b76d2250e780506106224/]
 

ruby myapp.rb -o 0.0.0.0 -p 5000 > /dev/null 2>&1 &

This will start the process in background and your endpoint on http://localhost:5000.

If you make a GET request on http://localhost:5000/your_username you will get a ‘success’ message, else a 404 status code.

Mock endpoint in Python

In Python it is very easy to create an endpoint with Flask.

To install it you have to run:

pip install flask-restful
[crayon-5b76d2250e78c711900315/]
 

As you can see, this does the exact same thing as the endpoint created in Ruby.

You simply run it with

python myapp.py

Mock endpoint in GO

[crayon-5b76d2250e793867605616/]
This does the same thing as the previous two endpoints and you run it with:

go run myapp.go

Thanks for reading this. What other ways of mocking an endpoint did you find?


How to Fix Common Errors When Testing in RSpec

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:
[crayon-5b76d225112ed394078063/]
Yep, that’s all. Now you can automate your tests and run them with the following command:
[crayon-5b76d225112f9884457015/]
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:
[crayon-5b76d225112ff656185878/]
[crayon-5b76d22511305828844689/]
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:
[crayon-5b76d2251130a491195822/]
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:
[crayon-5b76d22511310734815296/]
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.


How to (Easily) Debug a Rails Application

Hello all you party people! This article will show you how you can easily debug your Rails code.
If you're using the default Rails Rack server, Webrick - the best debugging tool I've been able to find out there is pry. Its use is pretty straightforward:

First off you'd need to add the pry gem to your Gemfile. Since this is used for development purposes, it should be added in the :development group:Read more


How I Came to Love Ruby

ruby_makes_me_happyIt wasn’t so long ago that I had no idea how everything worked in world of WWW. But that all changed when I got a technical support job at IntelligentBee. Working in a web development company (even though your job doesn’t have any tangency to the actual development role) opens a whole set of learning opportunities that will ultimately describe a whole new wonderful world. From that point on, my task was pretty simple: be curious about how stuff works and don’t be afraid to ask questions.

Why Ruby ? It's incredibly similar to English. Yes, to the actual English language. Read more


Create a mailer in Rails 4

Hello party people and polar bears! You were probably wondering how it is you send email using Ruby on Rails. Here's how you create an automated email system that sends a confirmation email to each new user that registers to your app:Read more


Setup a simple Ruby 2 on Rails 4 environment with Vagrant and Virtual Box

Since a developer time is most valuable when dealing with actual ... development tasks, setting up the environment for a new project better take less or no time, so that the developer can focus on building the actual product rather than focusing on setting up things.Read more