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.
You will get the following result.
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
Type the following command to install ‘selenium-webdriver’:
gem install selenium-webdriver
Example: If your directory name is ‘sample_scripts’ on desktop:
Let’s start with writing script in Rspec.
So for this open any editor. Here, I have used sublime.
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.
Description of the collection of related test cases
Individual test cases
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.