RockPaperScissors

LizardSpock

Rspec Testing Exceptions

| Comments

rspec is a testing framework designed to make test-driven development enjoyable and productive. On the recent release of RubyIt I wanted to improve the error reporting and cover this new functionality with tests.

Do not forget errors should be output to standard error not standard out, ie:

$stderr.puts 'Error Message'

The rspec documentation has section on error testing. This is my format of my current tests:

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    error1 = %{The error message}
    $stderr.should_receive(:puts).with( error1 ) 
    lambda { bowling.broken_method }.should raise_error(NameError)
  end
end

The $stderr.should_receive as well as setting up a test suppress the stderr outputting so that the output from the command line regression is clean.

Comments