Rspec Installation

Hello i am Amit K Kulkarni working as a QA Engineer in JoshSoftware Pvt Ltd.
Few months back i started working on Rspec and it was really great to see such a nice and
user friendly language.

Firstly let me start with what is RSPEC
RSpec is a behavior driven development framework for the Ruby programming language.
Behavior Driven Development (or BDD) is an agile software development technique that
encourages collaboration between developers,QA and non-technical or business participants
in a software project.

Features of BDD includes:
* Using examples to describe the behavior of the application

* Automating those examples to provide quick feedback and regression testing

* ‘should’ keyword is always used which works exactly with the behavior of the application

*Mocks can be used for modules of code which are yet to be written.

The RSpec tool is a Ruby package that lets you build a specification alongside your software.
This specification is actually a test that describes the behavior of your system.
Following is the flow of Rspec:

* You write a test. This test describes the behavior of a small element of your system.

* You run the test. The test fails because you have not yet built the code for that part of
your system. This important step tests your test case, verifying that your test case fails
when it should.

* You write code to make the test pass.

* You again run the tests and verify that they pass.

In essence, an Rspec developer turns test cases from red (failing) to green (passing) all day. It’s a motivating process.
Having learnt a bit about Rspec, lets go ahead with its installation

1: Firstly lets see whether rspec is installed or not.This can be checked by typing “gem list
rspec” in command prompt.It will display the gem and its version.If no gem appears then we
need to install the gem.This cab be done by typing the command :
“gem install rspec”

This will install spec gem.By default it will load the latest version of rspec.If you want to load a specific version then just suffix the command with -v(version no).
For e.g. gem install rspec -v=1.2.0

2: After rspec gem is installed successfully,run the command “gem install rspec-rails”
Rspec-rails is an RSpec extension that allows to drive the development of Ruby on Rails
applications with RSpec.

FEATURES of Rspec-rails

* We can independently specify Rails Models,Views , Controllers and Helpers.

* It also integrates Fixtures(plays an important role while testing which adds dummy data)
You can also specify the version which you want to install as specified above . So now Rspec
and Rspec-rails gem has been installed.
Just for confirmation about the version, In the command prompt type “spec -v” to cross
check the version of the rspec
* Before starting just have a look at the various commands of rspec by “spec –help”.
This will display all the spec commands which can be used in our application.It will give a idea
about various commands which are simple to use once you have got hang of it.

3: Now lets start by creating a new rails application by the command
“rails test_project”(name of the project)

4: After successful creation of project run the command from the root of your project
“ruby script/generate rspec”

5: This command creates spec folder in the application along with some files which are :
a: Rcov.opts
b: spec.opts
c: spec_helper.rb

6: Now rspec is generated and is integrated in our application.

7: Lets go ahead and create a model.For creating spec model go to the root directory of your
application and run command
ruby script/generate rspec_model User (model name)”

Be careful with model name.The first letter has to be capital. This command will automatically create a Models,Fixtures folders which inturn contains files i.e. user_spec.rb and its relevant
fixture users.yml. It also creates a migration file as well.

7: Similarly for controller creation type the command
“ruby script/generate rspec_controller user”

Here the first letter of the controller name should be small.In short whole name should be
in small case.After executing the command it will create a controllers,views and helpers folder which contains user controller file as user_controller_spec.rb (under controllers) ,
user_helper_spec.rb(under helpers)

8: While creating spec models or controllers rspec will search for its relevant model and
controller.If the model or controller is already created then it will automatically and skip
those files viceversa i.e. If relevant models or controllers are not created then it will
create those first and then create its spec models.

This was the basic installation of Rspec.I will also upload some basic code of models and controllers.

Note: While installing rspec i faced some problems which are mentioned below.
After successfull installation of Rspec gem i created a project and run the command to
generate rspec i.e. “ruby script/generate rspec”
But i got an error which says “couldn’t find rspec generator”. After searching i finally
got the solution which is mentioned below.

1: Install git(if not installed)
2: Open git command line
3: Go to the desired folder -> “vendor ->plugins ” which is created when you create your rails
project.
4: Run the following commands

4.1: “git clone git://github.com/dchelimsky/rspec.git”
4.2: “git clone git://github.com/dchelimsky/rspec-rails.git”

5: After successful installation run the above rspec generate command.

And the error was gone.
So folks this was the basic installation of Rspec and believe me it is one of the best testing script to use.
Give it a try and you will feel the same.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.