ROR Application Development Tips

I'm Ruby on Rails developer. I love to share my experience, technical knowledge. I work at Crypex Technologies which is developing applications in ROR for more than a decade.

Automated testing is extremely on the boom, across the board nowadays and obviously Ruby has its own arrangements to make it work for Ruby on Rails developers. One of these is RSpec, a madly prevalent tool for mechanized testing. The fortunate thing about it is we can test almost every part of RoR Application easily.

 

Rspec is easy to get it up and running, expressive way to write test cases, easy to use and also fast.

 

Let’s start with assembling all the components together to start with:

 

To begin it’s necessary to install Ruby. You can do it from here for Windows OS:  http://www.rubyinstaller.org

 

After completing installation let’s check if Ruby is installed properly along with version number.

 

  • For this go to command prompt and type:

                 ruby -v

 

        You will get the following result.

 

Automated Testing using RSpec and Selenium

 

  • Next, you need to install RSpec gem. Gem is Ruby library which can be used in one’s own code.
  • Go to command prompt and type the following :
       gem install rspec

 

       You will get the following output:

         

    Done installing documentation for diff-lcs, rspec-support, rspec-mocks, rspec-expectations, rspec-core, rspec after 22 seconds 6 gems installed

 

  • To start with Selenium in Ruby you also need to install the gem selenium-webdriver which will act as a binder for Ruby from the command line.

 

Type the following command to install ‘selenium-webdriver’:

 

gem install selenium-webdriver
  • Now, create directory i.e. folder to store Rspec scripts. Open your command prompt and change the directory path to the one where you would be saving your scripts.

Example: If your directory name is ‘sample_scripts’ on desktop:

 

C:\Desktop\sample_scripts>

 

Let’s start with writing script in Rspec.

 

So for this open any editor. Here, I have used sublime.

 

  • Write the following code in the sublime editor and save it. Save it with name sample.rb

 

Automated Testing Codes of RSpec & Selenium

 

 

  • To run the script go to command prompt and type:
C:\Desktop\sample_scripts>rspec sample.rb

 You will get the output as following after executing the script:

 

 Finished in 0.002 seconds (files took 0.11101 seconds to load)   1 example, 0 failures

 

Keywords in Rspec:

 

As this is targeted to new coders, you need to know some keywords which are meant to be used in your code. This helps in understanding the protocol to be followed while coding.

 

  • describe..... do

Description of the collection of related test cases

 

  • it......do

Individual test cases

 

  • Before() and After()

 

Optional test statements run before and after each or all test cases.

 

 

So, let’s hope you have understood the significance of using RSpec while automation testing.

 

This reduces your time to test & increases the efficiency of testing.

 

The code quality is needless to mention becomes high.

 

All these steps are for test-driven development (TDD) environment if you want to know how to use RSpec in Behaviour Driven Development (BDD) please refer our previous blog about it.

 

Aren’t we testers meant to have this as our focus? This is all about the beginning of using RSpec in your Ruby on Rails Development process.