# Functional tests : rescue\_action-rewrite causing problems (a

**URL:** <https://www.ruby-forum.com/t/functional-tests-rescue-action-rewrite-causing-problems-a/81609>\
**Category:** Rails\
**Created:** [January 10, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/functional-tests-rescue-action-rewrite-causing-problems-a/81609 "2007-01-10T17:04:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_del\_Stroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@Jonathan\_del\_Stroth](https://www.ruby-forum.com/u/Jonathan_del_Stroth)\
**Post date:** [January 10, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/functional-tests-rescue-action-rewrite-causing-problems-a/81609/1 "2007-01-10T17:04:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jonathan\_del\_Stroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@Jonathan\_del\_Stroth](https://www.ruby-forum.com/u/Jonathan_del_Stroth)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/functional-tests-rescue-action-rewrite-causing-problems-a/81609/2 "2007-01-19T16:30:39Z")

</div>

No takers?

---

<div class="post-metadata">

**Author:** ![Jonathan\_del\_Stroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@Jonathan\_del\_Stroth](https://www.ruby-forum.com/u/Jonathan_del_Stroth)\
**Post date:** [January 19, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/functional-tests-rescue-action-rewrite-causing-problems-a/81609/3 "2007-01-19T16:32:02Z")

</div>

2007/1/10, Jonathan del Strother [removed\_email\_address@domain.invalid](mailto: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://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![Jonathan\_del\_Stroth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@Jonathan\_del\_Stroth](https://www.ruby-forum.com/u/Jonathan_del_Stroth)\
**Post date:** [September 25, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/functional-tests-rescue-action-rewrite-causing-problems-a/81609/4 "2007-09-25T22:41:23Z")

</div>

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
