How to use RSpec's should_raise with any kind of exception?
expect { some_method }.to raise_error
RSpec 1 Syntax:
lambda { some_method }.should raise_error
See the documentation (for RSpec 1 syntax) and RSpec 2 documentation for more.
How do I `expect` something which raises exception in RSpec?
You could use the "rescue nil" idiom to shorten what you already have:
it { expect { eat(what: nil) rescue nil }.not_to change(cat, :status) }
RSpec 3: expect object.do_something to NOT raise a particular kind of error
This specific syntax solved the problem:
it 'do_something does not raise AError' do
begin
expect { object.do_something }.not_to raise_error
rescue RSpec::Expectations::ExpectationNotMetError => e
expect(e.message).not_to include 'AError'
end
end
How to raise an exception in an RSpec test
Like this, for example
expect(object).to receive(:save).and_raise(ActiveRecord::StaleObjectError)
bitly raise error exception rspec test
As you can see from the docs - https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/raise-error-matcher - the raise error matcher can take a second parameter of string or regex to match against the exceptions message
expect { whatever }.to raise_error(BitlyError, "ALREADY_A_BITLY_LINK")
Related Topics
How to Encode Media in Base64 Given Url in Ruby
Why Do People Say That Ruby Is Slow
How to Map/Collect with Index in Ruby
Tell Ruby Program to Wait Some Amount of Time
How to Invoke an Instance Method on a Ruby Module Without Including It
Rvm Is Not a Function, Selecting Rubies with 'Rvm Use ...' Will Not Work
How to Know What Is Not Thread-Safe in Ruby
Weird Imoperfection in Ruby Blocks
Shell Out from Ruby While Setting an Environment Variable
Ruby: Why Does Puts Call To_Ary
Missing Symbol When Installing Ruby-2.3.0 on Os X 10.11.6 by Rvm
What's the Point of Unary Plus Operator in Ruby
Ruby SQLite3 Installation SQLite3_Libversion_Number() MACos Sierra
Peer-To-Peer File Sharing with Web Sockets
Bundle Command Not Found. Bad Interpreter
Thread Safety: Class Variables in Ruby
What's the Difference Between Object and Basicobject in Ruby