Skip to content

Instantly share code, notes, and snippets.

@jherdman
Created February 27, 2013 16:53
Show Gist options
  • Save jherdman/5049474 to your computer and use it in GitHub Desktop.
Save jherdman/5049474 to your computer and use it in GitHub Desktop.
An RSpec matcher for checking HTTP response codes from a Rails Controller
RSpec::Matchers.define :have_status_code do |expected|
match do |response|
response.status == expected
end
description do
"be #{expected} (#{lookup_human_code(expected)})"
end
failure_message_for_should do |actual|
"expected response code to be #{expected} (#{lookup_human_code(expected)}) but was #{response.status} (#{lookup_human_code(response.status)})"
end
def lookup_human_code(code)
Rack::Utils::HTTP_STATUS_CODES[code]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment