Skip to content

Commit

Permalink
added preliminary handling for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Oct 26, 2008
1 parent 30e3a53 commit 9aff079
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
13 changes: 11 additions & 2 deletions lib/rspec-extensions/expectations.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
module RspecExtensions
module Expectations
def expect value
yield.should == value
def expect *args, &block
value = args.shift
if Class === value
if args.empty?
block.should raise_error(value)
else
block.should raise_error(value, args.first)
end
else
block.call.should == value
end
end
end
end
68 changes: 57 additions & 11 deletions spec/rspec-extensions/expectations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,64 @@
require File.join(File.dirname(__FILE__), %w[.. helper])

describe "expect value" do
describe RspecExtensions::Expectations do
include RspecExtensions::Expectations
it "should work just like should ==" do
expect 3 do
1 + 2

describe "expect value" do
it "should work just like should ==" do
expect 3 do
1 + 2
end
end
end

it "should fail just like should ==" do
lambda {
expect 3 do
1 + 1
it "should fail just like should ==" do
lambda do
expect 3 do
1 + 1
end
end.should raise_error(/expected: 3.*got: 2/m)
end
end

describe "expect error" do
describe "when the block raises the expected error" do
it "should pass with Exception" do
expect Exception do
raise "this error"
end
end
}.should raise_error(/expected: 3.*got: 2/m)
it "should pass with a subclass of the named exception" do
expect NameError do
raise 1.non_existent_method
end
end
end

describe "when the block raises the expected error and message" do
it "should pass" do
expect NameError, /undefined/ do
raise 1.non_existent_method
end
end
end

describe "when the block raises the expected error with the wrong message" do
it "should fail" do
lambda do
expect NameError, /wrong message/ do
raise 1.non_existent_method
end
end.should raise_error(/expected NameError/)
end
end

describe "when the block raises the wrong error" do
it "should fail" do
lambda do
expect NameError do
raise "this is not a name error"
end
end.should raise_error(/expected/)
end
end
end
end
end

0 comments on commit 9aff079

Please sign in to comment.