Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created August 28, 2012 01:30
Show Gist options
  • Save jimweirich/3494109 to your computer and use it in GitHub Desktop.
Save jimweirich/3494109 to your computer and use it in GitHub Desktop.
Spikes in Rspec/Given
# 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
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