Forum: Ruby on Rails Functional tests : rescue_action-rewrite causing problems (a

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
94c40fd67ffecc80b479aa9d7df3c494?d=identicon&s=25 Jonathan del Strother (Guest)
on 2007-01-10 17:04
(Received via mailing list)
Functional tests all come with a handy line that gives more sensible
exception reporting, looking something like :

class FooController; def rescue_action(e) raise e end; end

This can cause a few problems.  For example,
assert_response :missing  isn't going to work : the 404 status is no
longer set on getting an ActiveRecord::RecordNotFound.  Within
functional tests, this isn't too bad : you'd probably be content with
something like  assert_raises(ActiveRecord::RecordNotFound){get
'show', :id => 40000}


However, I'm struggling to deal with this and autotest.  AFAICT,
autotest doesn't reload the controllers when moving between
functional tests and integration tests, so controllers still have the
rewritten rescue_action method in integration.  So the exception gets
caught at dispatcher.rb:42, which always returns a status of 500.
The end result is that in autotest's integration testing, you might
get 404 or you might get 500, depending on whether or not your
functional tests got loaded first on that particular run.


Has anyone found a workaround for this?  At the moment I'm having to
remove the "def rescue_action(e) raise e end" line from functional
tests, and it's making functional test debugging a bit painful...

Jon
A9e5ce6f28587baa2a566eb4f2d76c13?d=identicon&s=25 Jon (Guest)
on 2007-01-19 16:30
(Received via mailing list)
No takers?
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2007-01-19 16:32
(Received via mailing list)
2007/1/10, Jonathan del Strother <maillist@steelskies.com>:
> However, I'm struggling to deal with this and autotest.  AFAICT,
> autotest doesn't reload the controllers when moving between
> functional tests and integration tests, so controllers still have the
> rewritten rescue_action method in integration.  So the exception gets
> caught at dispatcher.rb:42, which always returns a status of 500.
> The end result is that in autotest's integration testing, you might
> get 404 or you might get 500, depending on whether or not your
> functional tests got loaded first on that particular run.

This is causing problems even outside autotest.

rake test

will run functionals, and in the same Ruby process, run the
integration tests.  Most if not all controllers will have the
redefined rescue_action method...

The thing that should be done is to do it in setup, and replace that
in teardown.

A bit like the mocks do.  They replace a method, and on teardown, put
the method back into place.

Bye !

--
François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/
A9e5ce6f28587baa2a566eb4f2d76c13?d=identicon&s=25 Jon (Guest)
on 2007-09-25 22:41
(Received via mailing list)
Francois Beausoleil wrote:
> This is causing problems even outside autotest.
>
> rake test
>
> will run functionals, and in the same Ruby process, run the
> integration tests.  Most if not all controllers will have the
> redefined rescue_action method...
>
> The thing that should be done is to do it in setup, and replace that
> in teardown.


Just to see if anyone can find a cleaner way, this is what I'm using at
the moment in my test_helper :

unless ActionController::Base.method_defined?
:rescue_action_without_optional_raise
  class ActionController::Base
    attr_accessor :reraise_exceptions

    def rescue_action_with_optional_raise(e)
      if reraise_exceptions
        raise e
      else
        rescue_action_without_optional_raise(e)
      end
    end
    alias_method_chain :rescue_action, :optional_raise

    def initialize(opts={})
      self.reraise_exceptions = opts[:reraise_exceptions]
    end
  end
end

Then, in the functional tests' setup method, I can do  @controller =
PostsController.new(:reraise_exceptions => true), which will give me a
controller that gives a nice stack trace, without polluting
rescue_action's behaviour for integration testing.


Anyone spot a better way?

Jon
This topic is locked and can not be replied to.