Created
November 12, 2013 08:20
-
-
Save tcnksm/7427400 to your computer and use it in GitHub Desktop.
How to stub raising exceptions in rspec
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 Foo | |
class << self | |
def exec | |
something | |
rescue => ex | |
ex.message | |
end | |
def something | |
"something" | |
end | |
end | |
end | |
describe Foo do | |
describe ".exec()" do | |
context "when error occured" do | |
before do | |
Foo.stub(:something).and_raise(StandardError.new("error")) | |
end | |
subject(:msg) { Foo.exec } | |
it "returns its error message" do | |
expect(msg).to eq("error") | |
end | |
end | |
end | |
end |
In case anybody would be interested in how to stub that in today's world of rspec:
before do
expect(Foo).to receive(:something).and_raise(StandardError.new("error"))
end
Greetings from 2020 (yeah, THAT year).
and_raise(StandardError.new("error"))
haha, nice thank you :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case anybody would be interested in how to stub that in today's world of rspec:
Cheers!