Learning rspec!

In this blog, we will learn about rspec, a BDD (Behavioural Driven Development) for the Ruby Programming language. It is easy to learn and any person with some programming background can follow it.

Make sure you have Ruby installed.

Follow the below steps to install the rspec gem.

  • Open ‘git bash’.
  • Enter ‘gem install rspec’, it will automatically install all the dependencies.
  • Enter ‘rspec -v’ to see the version.
  • Make a folder, say ‘/Ruby_Testing’ and inside this folder create another folder called ‘spec’.
  • Create a small Hello-world class in Ruby as shown below.
Create a Ruby class called, 'hello_world.rb' with below content in it:

class Hello_World
  def say_hello
    "Hello World"
  end
end

Create another Ruby file called, 'hello_spec.rb' and fill in with:

require 'rspec'
require 'C:\..\..\Desktop\Temporary\spec\hello_spec'

describe Hello_World do
  it "when say_hello function is called, it should say Hello World" do
    wish = Hello_World.new
    expect(wish.say_hello).to eq "Hello W0rld"
  end
end

Now let's run the above code from gitbash command line:

rspec --format doc <test_spec.rb>

The above command will give the detailed output of the test result.

Failing_rspec

Now let's correct it and re-run the program:
require 'rspec'
require 'C:\..\..\Desktop\Temporary\spec\hello_spec'

describe Hello_World do
  it "when say_hello function is called, it should say Hello World" do
    wish = Hello_World.new
    expect(wish.say_hello).to eq "Hello World"
  end
end

Now we get the correct result:

Correct_rspec.png


Advertisements

2 thoughts on “Learning rspec!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s