Permalink
Browse files

added preliminary handling for errors

  • Loading branch information...
1 parent 30e3a53 commit 9aff079dcd608269e567b61cc8ca6995fbb56af9 @dchelimsky committed Oct 26, 2008
Showing with 68 additions and 13 deletions.
  1. +11 −2 lib/rspec-extensions/expectations.rb
  2. +57 −11 spec/rspec-extensions/expectations_spec.rb
@@ -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
@@ -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.