Functional tests : rescue_action-rewrite causing problems (a


#1

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


#2

No takers?


#3

2007/1/10, Jonathan del Strother removed_email_address@domain.invalid:

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/


#4

Francois B. 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