Created
August 28, 2012 01:30
-
-
Save jimweirich/3494109 to your computer and use it in GitHub Desktop.
Spikes in Rspec/Given
This file contains hidden or 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
# Spike in rspec-given | |
# The following now works in a proof of concept spike. | |
# When an exception happens during a When block (and there | |
# result variable to catch it), the result will be a "Touchy" | |
# object that re-raises the exception from the When block | |
# on every single method you could call on it. | |
describe "Empty Stack" do | |
When(:result) { empty_stack.pop } | |
# This fails because calling .nil? on the touchy object | |
# re-raises the exception. | |
Then { result.should be_nil } | |
# This passes because when the raise_error matcher attempts | |
# to evaluate what it thinks is a lambda (but really is a Touchy | |
# object) the expected exception is raised). | |
Then { result.should raise_error(Stack::UnderflowError, /empty/) } | |
# The raise_error matcher "reads funny" when given a result. We | |
# could easily setup an alias for raise_error. | |
Then { result.should have_failed(Stack::UnderflowError, /empty/) } | |
end |
This file contains hidden or 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
class Touchy < BasicObject | |
def initialize(exception) | |
@exception = exception | |
end | |
def method_missing(sym, *args, &block) | |
::Kernel.raise @exception | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment