Getting introduced to BDD using RSpec

March 24, 2009

Today I had a great opportunity to attend one of the local Ruby User Groups in Cleveland OH. It was a very interesting experience where a group of people were trying to solve a problem. The problem that they were trying to solve is to implement a bowling calculation class using BDD and RSpec. The group consisted of 6 people but only 2 people were working on a problem at any point and time where one person would sit down and write the test and hand the keyboard and mouse to the other person and have them write the minimal amount of code to make the test pass. After 10 minutes of going back and forward the person who was writing the code to pass the tests walked away from the computer and the person writing the tests ended up in the driver seat to write the code and a new person would come in to write the tests.
I was amazed at how easy it was to write the test and then have to write the code to satisfy the test.
I have made my first attempt at writing my very first test and below you can see the results of a very interesting factory class and a RSpec test that I ended up writing.

In order to run the code that I have below you will have to make sure that you have RubyGems and RSpec gem installed.

   1: # factory_spec.rb

   2: require 'rubygems'

   3: gem 'rspec'

   4: require 'factory'


   6: describe "should test factory's output" do

   7:     before(:each) do

   8:         @factory =

   9:         @factory.make_cars(0)

  10:     end


  12:     it "should return 0 when 0 is passed as an argument" do

  13:         @factory.daily_output.should == 0

  14:     end

  15: end

Here is the sample class that I wrote in order to satisfy the test that is written above:

   1: #factory.rb

   2: class Factory

   3:     def make_cars(carcount)

   4:         @blah = carcount

   5:     end

   6:     def daily_output

   7:         @blah

   8:     end

   9: end

Then to run the code below you will have to execute the following command:

   1: spec -f s --c factory_spec.rb

And the output of the above test looks like this:

   1: ilya@RubyDev:~/projects/ruby/testing$ spec -f s --c factory_spec.rb


   3: should test factory's output

   4: - should return 0 when 0 is passed as an argument


   6: Finished in 0.003434 seconds


   8: 1 example, 0 failures

After having to get my hands dirty writing this test things all of a sudden started making sense. Having to write tests before writing the code seems like a lot of work for a beginner TDD or BDD coder but after some time the benefits of having to think about the requirement and then coding makes you think about the problem at hand and then appropriately coding a solution.

If you have any comments please do not hesitate to respond and I can try my best to answer any questions.


Leave a Reply

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

You are commenting using your 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

%d bloggers like this: