30

I use HTTP status code symbols in code in a controller such as:

render json: {
    auth_token: user.authentication_token, 
    user: user
  }, 
  status: :created

or

render json: {
    errors: ["Missing parameter."]
  }, 
  success: false, 
  status: :unprocessable_entity

In the code of my request spec I also would like to use the symbols:

post user_session_path, email: @user.email, password: @user.password
expect(last_response.status).to eq(201)

...

expect(last_response.status).to eq(422)

However each test where I use the symbols instead of integers fails:

Failure/Error: expect(last_response.status).to eq(:created)

  expected: :created
       got: 201

  (compared using ==)

Here is the latest list of HTTP status code symbols in Rack.

sameers
  • 4,447
  • 2
  • 28
  • 42
JJD
  • 44,755
  • 49
  • 183
  • 309

4 Answers4

26

The response object responds to several of the symbol types as messages. So you can simply do:

expect(response).to be_success
expect(response).to be_error
expect(response).to be_missing
expect(response).to be_redirect

For the other types, such as :created, you can create a simple custom matcher for this which wraps assert_response:

RSpec::Matchers.define :have_status do |type, message = nil|
  match do |_response|
    assert_response type, message
  end
end

expect(response).to have_status(:created)
expect(response).to have_status(404)

This should work fine for controller specs which have the proper state setup. It will not work for feature specs. I haven't tried with request specs, so your milage may vary there.

The reason this works is it leverages the fact that RSpec controller specs have similar state setup behind the scenes. So when assert_response accesses @response it is available.

This matcher can probably be improved by simply copying the code used by assert_response into the matcher:

RSpec::Matchers.define :have_status do |type, message = nil|
  match do |response|
    if Symbol === type
      if [:success, :missing, :redirect, :error].include?(type)
        response.send("#{type}?")
      else
        code = Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
        response.response_code == code
      end
    else
      response.response_code == type
    end
  end

  failure_message do |response|
    message or
      "Expected response to be a <#{type}>, but was <#{response.response_code}>"
  end
end

UPDATE: 2014-07-02

This is now available out of the box with RSpec Rails 3: https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/matchers/have-http-status-matcher

Aaron K
  • 6,528
  • 3
  • 31
  • 28
21

this works for me:

expect(response.response_code).to eq(Rack::Utils::SYMBOL_TO_STATUS_CODE[:not_found])
cyrilchampier
  • 2,077
  • 1
  • 21
  • 37
12

On the one hand, response is built with methods like:

  • success?

  • redirect?

  • unprocessable?

  • full list do: response.methods.grep(/\?/)

On the other hand, Rspec predicates transforms every foo? method to a be_foo matcher.

Not sure you can have the 201 this way unfortunately, but creating a custom matcher is quite easy.

Note Rails test only rely on a few statuses.

apneadiving
  • 110,730
  • 25
  • 209
  • 207
  • I tried `expect(last_response.status).to be_created` which fails with *"undefined method `created?' for 201:Fixnum"*. – JJD Aug 16 '13 at 14:45
  • 1
    `expect(last_response).to be_success` – apneadiving Aug 16 '13 at 14:45
  • Same: `expect(last_response.status).to be_success` fails with *"undefined method 'success?' for 201:Fixnum"*. I understood that `created?` is not part of the list and experimented with a custom matcher ... w/o success by now though. – JJD Aug 16 '13 at 14:47
  • reread what I wrote: `expect(last_response)` not `expect(last_response.status)` – apneadiving Aug 16 '13 at 14:56
  • I admin I did not read your comments so well but this time it fails with *"undefined method `success?' for #"* – JJD Aug 16 '13 at 15:01
  • why do you have a `` it should be a `ActionController::TestResponse`. where do your specs live? – apneadiving Aug 16 '13 at 15:03
  • The request test I am executing is located in `spec/api/sessions_controller_spec.rb`. – JJD Aug 16 '13 at 15:07
  • 1
    request spec? request specs should live in `spec/features`, controller spec should live in `spec/controllers`, otherwise the wrong modules will be added (unless you include them manually) – apneadiving Aug 16 '13 at 15:09
  • I might misunderstand but [rspec-rails states](https://github.com/rspec/rspec-rails#request-specs) that request specs belong either in `spec/requests`, `spec/api` or `spec/integration`. – JJD Aug 16 '13 at 15:14
  • from your link: `Note that Capybara's DSL as shown is, by default, only available in specs in the spec/features directory` – apneadiving Aug 16 '13 at 15:24
  • Thanks. But when I move the test into `spec/features` it fails with `undefined local variable or method 'app' for # – JJD Aug 16 '13 at 15:42
  • in a feature spec you should not bother about `response`, you should work on `page`. `response` is for controllers spec. – apneadiving Aug 16 '13 at 15:43
  • I dont what and how you did but when you do not follow the standard paths, it's normal you stumble upon many issues/ – apneadiving Aug 16 '13 at 15:44
  • I guess I am on the wrong way. Can you point to the correct path please to test the response of my API endpoints? – JJD Aug 16 '13 at 15:45
  • This shoul be controller specs, and you'd be able to use what I put in my answer – apneadiving Aug 16 '13 at 16:03
  • I am trying to follow your advise and [tutorials like this](http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html) but I still can't get it running. To avoid further drifting away from my original question [here is a new post](http://stackoverflow.com/questions/18278326/nameerror-undefined-local-variable-or-method-app). – JJD Aug 16 '13 at 16:48
  • I've spent much time on your question without any counterpart, don't expect me to spend more elsewhere. sorry. – apneadiving Aug 16 '13 at 18:43
  • No offense please. I am the last one who forgets to vote on people's help. Though, normally I wait until I the problem is solved in total before I vote or grant the answer flag. – JJD Aug 16 '13 at 21:45
11

With rspec-rails (as of rspec 3) it's possible to use

expect(response).to have_http_status(:created)

Update 2018-06-11:

As of Rails 6, some of the matchers will be replaced (e. g. success by successful).

schmijos
  • 6,629
  • 3
  • 43
  • 50