RSpec 0.8.1 is out. Out of any expectations!

Posted by mklishin on March 02, 2007
Ruby developers beloved BDD framework RSpec goes 0.8.1. This is actually a bugfix release after 0.8 one day earlier. It has new expectation syntax although old one is here to stay up to 0.9. But what is much more interesting, now we have a way to add our own expectations and matchers following simple methods overriding rules.

class BeInZone
   def initialize(expected)
      @expected = expected
   end
   def matches?(actual)
      @actual = actual
      bob.current_zone.eql?(Zone.new(@expected))
   end
   def failure_message
      "expected #{@actual.inspect} to be in Zone #{@expected}"
   end
   def negative_failure_message
      "expected #{@actual.inspect} not to be in Zone #{@expected}"
   end
end

def be_in_zone(expected)
    BeInZone.new(expected)
end

and use it like this:

module CustomGameMatchers
    class BeInZone
      ...
    end

    def be_in_zone(expected)
      ...
    end
end

context "Player behaviour" do
    include CustomGameMatchers
    ...
end

Given Ruby DSL power this may lead to some unpredictable addiction to BDD and Ruby in general. Don't forget to run script/generate rspec as you're done with installation of Rails plugin from tag 0.8.1 cause it generates one more file for run preferences. I said preferences? Yes, like colored output. Passed specs are now green in Bash, messages are purple and failures are red. Really easy to recognize after months of black and white. The same day Mauricio Fernandez posted announcement of RCov 0.8, code coverage tool for Ruby that works with RSpec. Not that bad... And don't forget to read new documentation to RSpec, a lot of stuff changed there but everything is still easy to learn and specs look even closer to human written requirements text. What else could we ask BDD framework of?