-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added preliminary handling for errors
- Loading branch information
1 parent
30e3a53
commit 9aff079
Showing
2 changed files
with
68 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |