Skip to content

Instantly share code, notes, and snippets.

@gerhard
Forked from floehopper/thing.rb
Created January 24, 2014 13:58
Show Gist options
  • Save gerhard/8597750 to your computer and use it in GitHub Desktop.
Save gerhard/8597750 to your computer and use it in GitHub Desktop.
class Thing
def execute
stubbed_method
end
end
MyException = Class.new(StandardError)
describe Thing do
subject(:thing) { Thing.new }
it 'does something' do
thing.stub(:stubbed_method, &on_first_invocation_raise(MyException))
expect { thing.execute }.to raise_error(MyException)
expect { thing.execute }.to_not raise_error
expect(thing).to have_received(:stubbed_method).twice
end
private
def on_first_invocation_raise(exception = StandardError)
lambda do
unless @invoked
@invoked = true
raise exception
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment