[rspec-devel] [ rspec-Feature Requests-6265 ] should_raise should accept an Exception object

noreply at rubyforge.org noreply at rubyforge.org
Fri Nov 10 20:26:40 EST 2006


Feature Requests item #6265, was opened at 2006-10-22 01:46
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6265&group_id=797

Category: mock module
Group: None
>Status: Closed
Priority: 3
Submitted By: Chad Woolley (thewoolleyman)
>Assigned to: Aslak Hellesøy (aslak_hellesoy)
Summary: should_raise should accept an Exception object

Initial Comment:
So I can pass it an exception object with the message (or anything else) set on it.

----------------------------------------------------------------------

>Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-11-10 20:26

Message:
Thanks! Fixed in r1090.

----------------------------------------------------------------------

Comment By: Chad Woolley (thewoolleyman)
Date: 2006-11-10 18:59

Message:
Just attached a patch and passing spec for this.  Works for
me (tm), should be backward compatible.

----------------------------------------------------------------------

Comment By: Chad Woolley (thewoolleyman)
Date: 2006-11-09 03:53

Message:
Reopening this.  I'll try to submit a patch for this soon.

----------------------------------------------------------------------

Comment By: Chad Woolley (thewoolleyman)
Date: 2006-11-09 03:51

Message:
OK, here's a failing spec which illustrates what I'm talking
about.  Basically, I want to provide my own pre-created
exception to be thrown, with a specified error message.


require 'rubygems'
require 'spec'

context "a mock" do
  specify "should allow a pre-created exception to be raised" do
    error_message = "msg"
    e = RuntimeError.new(error_message)
    errormock = mock("Mock")
    errormock.should_receive(:method).and_raise(e)
    errmsg = nil
    begin
      errormock.method
    rescue RuntimeError => e
      errmsg = e.message
    end
    errmsg.should_equal(error_message)
  end
end  

----------------------------------------------------------------------

Comment By: Chad Woolley (thewoolleyman)
Date: 2006-10-24 02:51

Message:
Yes, I meant "and_raise", it was a typo.  I will try the
Proc#should_raise and see if that works.

Thanks,
Chad

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-10-22 08:42

Message:
I'm a little confused. You categorised this RFE in the mock module. RSpec mocks don't have a sould_raise method, but an and_raise method (which allows you to pass in an exception)

http://rspec.rubyforge.org/documentation/mocks.html
http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/spec/spec/mocks/mock_spec.rb?revision=931&root=rspec

Or perhaps you're talking about the Proc#should_raise method? It also allows you to pass an exception object:

http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/spec/spec/expectations/should/should_raise_spec.rb?root=rspec&view=co
http://rspec.rubyforge.org/documentation/expectations.html

If this doesn't work for you, please submit a failing spec.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6265&group_id=797


More information about the rspec-devel mailing list