[rspec-users] specifying an HTTP status code

Scott Taylor mailing_lists at railsnewbie.com
Sat Dec 1 16:27:53 EST 2007

On Dec 1, 2007, at 3:45 PM, Francis Hwang wrote:

> Hi all,
> I'm just starting to work w/ RSpec, so I hope this question isn't too
> obvious or missing the point somehow: Is there a way in RSpec to
> specify that a controller action should use a specific HTTP status
> code? Specifically I want to test for the usage of 301 as opposed to
> 302, for a permanent redirection.
> response.should be_redirect
> looks like it calls ActionController::TestRequest#redirect?, which is
> only testing for a status code from 300..399. In an old-fashioned
> Test::Unit Rails test I could do assert_response, but I can't see
> anything matching that in RSpec. Is there a less-documented feature
> I'm missing?
> Thanks,
> Francis Hwang
> http://fhwang.net/

Hey Francis - Great to see you've finally come over from the dark  
side (Test::Unit)!  How is rspec treating you?

Anyway - here's how I've done it with a 404:

describe ErrorController, "view" do

   it "should render the 404 page in public/" do
     get :view
     response.should render_template("#{RAILS_ROOT}/public/404.html")

   it "should return an HTTP status code of 404" do
     get :view
     response.headers["Status"].should == "404 Not Found"


I would imagine you could do something similar for 301.  I don't  
think there are currently any matchers for these status codes like:  
should_be_not_found or should_not_be_a_404, although they could  
certainly be written.  (Method missing seems to be screaming at me  


Scott Taylor

More information about the rspec-users mailing list