Created
April 3, 2013 19:59
-
-
Save rogeliog/5304707 to your computer and use it in GitHub Desktop.
Replaces any given exception that happens inside a block with a new one. @garybernhardt "if this call raises exception Foo, I want you to raise exception Bar".
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
def replace_exception original_exception, new_exception | |
begin | |
yield | |
rescue original_exception | |
raise new_exception | |
end | |
end | |
class OtherException < Exception; end | |
class UselessException < Exception; end | |
class SomeException < Exception; end | |
describe 'Replace exception' do | |
context 'When the original exception is raised' do | |
it 'Raises the new exception' do | |
lambda do | |
replace_exception SomeException, OtherException do | |
raise SomeException | |
end | |
end.should raise_error OtherException | |
end | |
end | |
context 'When no exception is raised' do | |
it 'Does not raise exception' do | |
lambda do | |
replace_exception SomeException, OtherException do | |
end | |
end.should_not raise_error | |
end | |
end | |
context 'When the original exception is not raised' do | |
it 'Does not raise the new exception' do | |
lambda do | |
replace_exception SomeException, OtherException do | |
raise UselessException | |
end | |
end.should raise_error UselessException | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment