# Can't seem to test http error codes

**URL:** <https://www.ruby-forum.com/t/cant-seem-to-test-http-error-codes/153470>\
**Category:** Rails\
**Created:** [November 17, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/cant-seem-to-test-http-error-codes/153470 "2008-11-17T17:39:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davetron5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@davetron5000](https://www.ruby-forum.com/u/davetron5000)\
**Post date:** [November 17, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/cant-seem-to-test-http-error-codes/153470/1 "2008-11-17T17:39:41Z")

</div>

in application.rb

rescue\_from ActiveRecord::RecordNotFound { |e| http\_status\_code  
(:missing, e) }

def http\_status\_code(status, exception)  
@exception = exception  
respond\_to do |format|  
format.html { render :template =\> “shared/status\_#  
{status.to\_s}”, :status =\> status }  
format.any { head status }  
end  
end

in a controller

```
project = Project.find(params[:id])

```

in my test

def test\_bad\_project  
put :update, :id =\> KNOWN\_BAD\_ID  
assert\_response(:missing)  
end

The result is that I get a 500 every time.

Any clues as to whats’ going on? There’s nothing in the log to  
explain the 500 I get during tests; the application controller seems  
to be detecting the exception and setting the status to 404, but my  
tests get 500.

Dave

---

<div class="post-metadata">

**Author:** ![davetron5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@davetron5000](https://www.ruby-forum.com/u/davetron5000)\
**Post date:** [November 17, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/cant-seem-to-test-http-error-codes/153470/2 "2008-11-17T17:54:58Z")

</div>

On 17 Nov 2008, at 16:39, davetron5000 wrote:

> {status.to\_s}", :status =\> status }  
> format.any { head status }  
> end  
> end

IIRC the test harness snarfs the exception before it gets to your  
exception handler.

Fred

---

<div class="post-metadata">

**Author:** ![davetron5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@davetron5000](https://www.ruby-forum.com/u/davetron5000)\
**Post date:** [November 17, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/cant-seem-to-test-http-error-codes/153470/3 "2008-11-17T19:27:41Z")

</div>

The exception handler IS getting called (I put log statements in and  
they appear in test.log).

In previous versions of rails, there seemed to be some sort of magic  
where a non-200 http response gets turned into a 500 for development,  
but not production. Is that the case, and if so, how do I deal with  
this? I need to test that the correct http responses are being  
sent…

Dave

On Nov 17, 11:54 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
