[rspec-devel] [ rspec-Bugs-10744 ] should raise_error not working in new controller specs

noreply at rubyforge.org noreply at rubyforge.org
Sun May 13 19:02:27 EDT 2007


Bugs item #10744, was opened at 2007-05-10 16:26
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=10744&group_id=797

Category: None
Group: None
Status: Closed
Resolution: Rejected
Priority: 3
Submitted By: Paul Pagel (paulwpagel)
Assigned to: Nobody (None)
Summary: should raise_error not working in new controller specs

Initial Comment:
I have the following spec

  it "Should raise exception when ***" do
    lambda{post :index, :key => '987'}.should raise_error(Exception)
  end

which fails because of

'The LoginController Should raise exception when packet is not found' FAILED
expected Exception but nothing was raised
login_controller_spec.rb:42:

and in the controller i have an index method which does throw an exception, which I can verify by wrapping it in a begin/rescue block, but the spec still fails.

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

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-05-13 23:02

Message:
FYI - with the addition of Global before and after (http://rubyforge.org/tracker/?func=detail&atid=3152&aid=10753&group_id=797), you can now do this:

Spec::Runner.configure do |config|
  config.before(:each, :behaviour_type => :controller) do
    controller.class.send(:define_method, :rescue_action) do |e|
      raise e
    end
  end
end

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

Comment By: David Chelimsky (dchelimsky)
Date: 2007-05-12 12:42

Message:
By default, Rails swallows exceptions and returns error codes in headers (see actionpack/lib/action_controller/rescue.rb).

Controller specs in 0.8.x and earlier used to override the default behaviour like this:

@controller_class.send(:define_method, :rescue_action) { |e| raise e }

This would cause errors thrown in actions to be thrown in the specs. Unfortunately, it also overrode custom rescue_actions (see http://rubyforge.org/tracker/?group_id=797&atid=3149&func=detail&aid=9767).

I'm closing this as this is now the expected behaviour. I've added the following rdoc for ControllerBehaviour:

# == Expecting Errors
#
# By default, Rails will swallow errors that are raised in controller
# actions and return an error code in the header. If you want to
# specify that an action should raise an error, you can either
# override rescue_action in your controller ...
#
#   before(:each) do
#     controller.class.send(:define_method, :rescue_action) { |e| raise e }
#   end
#
# ... or you can expect error codes in headers ...
#
#   it "should return an error in the header" do
#     response.should be_error
#   end
#
#   it "should return a 501" do
#     response.response_code.should == 501
#   end
#
#   it "should return a 501" do
#     response.code.should == "501"
#   end



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

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-05-10 19:54

Message:
Does your spec fail if you remove the lambda and just do:

post :index, :key => '987'

How can we reproduce this? Please attach a failing example

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

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-05-10 19:52

Message:
Before investigating this we need to know the versions of all 
relevant software you are using, for example:

* OS X 10.4.9
* Ruby 1.8.6
* RSpec 1.8.2 or RSpec trunk r1643
* Spec::Rails 1.8.2 or Spec::Rails tag r1234...
* Rails 1.2.3 or Rails edge r5645
* Whatever other relevant software.

Without this information it's like solving 1 equation (your 
question) with 5 unknown variables (the versions).

We strongly recommend that you use the latest released version of
RSpec, alternatively the subversion trunk (if you're the bleeding
edge kind of person).

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

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


More information about the rspec-devel mailing list