Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 738 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use RSpec's should_raise with any kind of exception?

#1
I'd like to do something like this:

some_method.should_raise <any kind of exception, I don't care>

How should I do this?

some_method.should_raise exception

... doesn't work.
Reply

#2
The syntax changed recently and now it is:

expect { ... }.to raise_error(ErrorClass)
Reply

#3
Instead of lambda, use expect to:

expect { some_method }.to raise_error

This is applies for more recent versions of rspec, i.e. rspec 2.0 and up.

See the <a href="http://relishapp.com/rspec/rspec-expectations/v/2-0/dir/matchers/expect-error">doco</a> for more.
Reply

#4
expect { some_method }.to raise_error

**RSpec 1 Syntax:**

lambda { some_method }.should raise_error

See [the documentation][1] (for RSpec 1 syntax) and [RSpec 2 documentation][2] for more.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
### RSpec 2

expect { some_method }.to raise_error
expect { some_method }.to raise_error(SomeError)
expect { some_method }.to raise_error("oops")
expect { some_method }.to raise_error(/oops/)
expect { some_method }.to raise_error(SomeError, "oops")
expect { some_method }.to raise_error(SomeError, /oops/)
expect { some_method }.to raise_error(...){|e| expect(e.data).to eq "oops" }

# Rspec also offers to_not:
expect { some_method }.to_not raise_error
...

Note: <code>raise_error</code> and <code>raise_exception</code> are interchangeable.


### RSpec 1

lambda { some_method }.should raise_error
lambda { some_method }.should raise_error(SomeError)
lambda { some_method }.should raise_error(SomeError, "oops")
lambda { some_method }.should raise_error(SomeError, /oops/)
lambda { some_method }.should raise_error(...){|e| e.data.should == "oops" }

# Rspec also offers should_not:
lambda { some_method }.should_not raise_error
...

Note: <code>raise_error</code> is an alias for <code>raise_exception</code>.


### Documentation:

[To see links please register here]


RSpec 2:

*

[To see links please register here]


RSpec 1:

*

[To see links please register here]

*

[To see links please register here]

Reply

#6
From version 3.3 on `rspec-expections` gem raises a warning for a blank raise_error without a parameter

expect { raise StandardError }.to raise_error # results in warning
expect { raise StandardError }.to raise_error(StandardError) # fine

This gives you a hint that your code may fail with a different error than the test intended to check.

> WARNING: Using the `raise_error` matcher without providing a specific error or message risks false positives, since `raise_error` will match when Ruby raises a `NoMethodError`, `NameError` or `ArgumentError`, potentially allowing the expectation to pass without even executing the method you are intending to call. Instead consider providing a specific error class or message. This message can be supressed by setting: `RSpec::Expectations.configuration.warn_about_potential_false_positives = false`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through