-
-
Save gerhard/8597750 to your computer and use it in GitHub Desktop.
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
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