[rspec-users] no should raise_exception

Pat Maddox mailinglists at patmaddox.com
Wed Dec 23 11:35:03 EST 2009


On Dec 22, 2009, at 2:08 PM, David Chelimsky wrote:

> On Tue, Dec 22, 2009 at 9:33 AM, rogerdpack <rogerpack2005 at gmail.com> wrote:
> > raise_error already catches any type of exception, error or not:
> >
> >   class BlahException < Exception; end
> >   class BlahError < StandardError; end
> >
> >   lambda { raise BlahException }.should raise_error(BlahException)
> >   lambda { raise BlahError }.should raise_error(BlahError)
> >   lambda { raise "blah" }.should raise_error(RuntimeError, "blah")
> 
> Thanks for the response.  I think my request was more of a "why call
> them errors--in my head one doesn't raise errors--one raises
> exceptions and interprets them as errors, so allowing for the syntax
> raise_exception would be more mind friendly to me."
> 
> What I really want to say is "should raise(Blah)" but Ruby already defines raise as a keyword :)
> 
> I'd be open to aliasing raise_error with raise_exception, renaming it to raise_exception and aliasing raise_error for compatibility, but I think this might just add confusion rather than clarifying intent. Thoughts?

lambda { raise BlahException }.should raise_error(BlahException)

is perfectly readable.  I don't see why you would need to change anything to make it more so.  An alias provides no extra value.  And raise_error should only pass on StandardError or subclasses if no class is specified.  Ruby makes you specify the class if it's an Exception, and Ruby semantics should be RSpec defaults.

Pat

p.s. I don't know if raise_error should even allow you to skip the error type.  I've seen way too many tests that look like

it "should raise an error" do
  lambda {
    foo.stupid_typo_that_will_raise_a(NoMethodError)
    foo.some_method_that_raises(TheRealError)
  }.should raise_error
end

But that's another thread
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091223/7967ea19/attachment.html>


More information about the rspec-users mailing list