Thursday, April 16, 2009

Another thing to love about RSpec

I’ve been busy working on refactoring our test framework, and I ran across a gem (spoken in the usual sense, not in the RubyGem sense).   

RSpec has a method for running a block of code before and after each example, before(:each) and after(:each).  Its syntax is pretty simple:

describe “this is my feature” do

  before(:each)
    do_this_before_each_example()
  end
  after(:each)
    and_do_this_at_the_end_of_each_example()
  end
  it “should be example 1” do
  end
  it “should be example 2” do
  end

end

This is pretty standard stuff.  The cool part is you can also use a before(:all) and after(:all) method to do stuff (like major setup and teardown) once at the beginning of the describe block, and the after(:all) at the very end.  Since it can be tied to any describe block, it’s completely flexible how big a block of example it applies to.

The more I use Ruby for testing…the less reason I have for looking at other languages.

No comments:

Post a Comment