Behavior Driven Development

Some months ago I visited Aslak Hellesøys' RSpec presentation at RubyEnRails 2007. I was really intrigued by some of the concepts introduced by RSpec.

After checking out the way testing/TDD works in Grails I just had to see if my initial interest in RSpec would stand ground.

Just to recapitulate on what RSpec is:

RSpec is a framework which provides programmers with a Domain Specific Language to describe the behavior of Ruby code with readable, executable examples that guide you in the design process and serve well as both documentation and tests.

So RSpec gives a developer the means to write tests in a form that matches specification of functional requirements dictated by the customer. They call it behavior driven design.

Conceptually I think this is a good thing. Quite often customers don't actually know what is being tested in their project. Just look at one of the basic methods most often used in testing: assertNotNull. How does a customer know what this is supposed to test? The most obvious answer to this is: documentation/comments... which to be frankly isn't something most developers excel in.

Now, what does it look like... and how do you use it?

Installing rspec and its' Rails plugin is a breeze:

CODE:
  1. bash> sudo gem install rspec
  2. bash> cd {my_rails_project}
  3. bash> ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
  4. bash> ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails

After installing the plugins in your Rails project a couple of new rake tasks are available (prefixed with spec:) to create run tests for your models/views/controllers/helpers etc.

RSpec also install some generators: rspec, rspec_controller, rspec_model, rspec_scaffold. These generators are similar to the default Rails counterparts but generate corresponding RSpec tests as opposed to unittests.

Let's get started.

Say we have a really simple specification for a model: "if a new article is created its' description field should contain 'no description entered'".

So, let's start with creating a simple model:

CODE:
  1. bash>./script/generate rspec_model Article title:string, description:string

This will give us, next to the expected model file and migration, a file called article_spec.rb in the spec directory:

RUBY:
  1. require File.dirname(__FILE__) + '/../spec_helper'
  2.  
  3. describe Article do
  4.   before(:each) do
  5.     @article = Article.new
  6.   end
  7.  
  8.   it "should be valid" do
  9.     @article.should be_valid
  10.   end
  11. end

Which we can run immediately:

CODE:
  1. bash>rake spec
  2. (in /Users/peter/Development/rails/brs)
  3. .
  4.  
  5. Finished in 0.045879 seconds

And, we can also create a simple overview of the specifications which we test:

CODE:
  1. 1 example, 0 failures
  2. bash>rake spec:doc
  3. (in /Users/peter/Development/rails/brs)
  4.  
  5. Article
  6. - should be valid

Cool, now let's add our specification:

RUBY:
  1. it "the description attribute should contain 'no description entered'" do
  2.   @article.should == "no description entered"
  3. end

Running the tests results in an error now:

CODE:
  1. 'Article the description attribute should contain 'no description entered'' FAILED
  2. expected: "no description entered",
  3.      got: nil (using ==)

Setting the default value for the description attribute of Article like this:

RUBY:
  1. class Article <ActiveRecord::Base
  2.   def initialize(attributes = {})
  3.     super(attributes)
  4.     write_attribute(:description, "no description entered")
  5.   end
  6. end

Results in a successful test. And running spec:doc now tells me I have a test for this specification:

CODE:
  1. bash>rake spec:doc
  2. (in /Users/peter/Development/rails/brs)
  3.  
  4. Article
  5. - should be valid
  6. - the description attribute should contain 'no description entered'

Conclusion
Although this is a really basic example (and there is much more the RSpec) I think I got the point.
I really like the "behavior driven" way of thinking; it provides clear handles for writing tests and creates useful overviews of what is actually tested.


0 Responses to “Behavior Driven Development”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


Scribbled Sun Logo javaone 2008 goodybag Greenland pub Rudie smashmouth Joshua Bloch at JavaOne2008 Community One Keynote Okke en Rudie alcatraz Charles Nutter & Guillaume Laforge sea_lion Hotel room nearby hotel Acme Anvile at CommunityOne Keynote Cable Car line Golden Gate Tim Bray introducing the (J)Ruby panel Stage being build in the nearby park golden_gate_warning_sign
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.