Skip to content

Instantly share code, notes, and snippets.

@ArthurZheng
Forked from tcnksm/exception_spec.rb
Created November 14, 2021 09:34
Show Gist options
  • Save ArthurZheng/21c608001b09d8171b9942dd0115308d to your computer and use it in GitHub Desktop.
Save ArthurZheng/21c608001b09d8171b9942dd0115308d to your computer and use it in GitHub Desktop.
How to stub raising exceptions in rspec
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment