-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Add better debuggability to be_forbidden failures | ||
RSpec::Matchers.define :be_forbidden do | ||
match(&:forbidden?) | ||
|
||
failure_message do |actual| | ||
debug_text_for_failure('forbidden', response: actual, last_request: last_request) | ||
end | ||
end | ||
|
||
# Add better debuggability to be_not_found failures | ||
RSpec::Matchers.define :be_not_found do | ||
match(&:not_found?) | ||
|
||
failure_message do |actual| | ||
debug_text_for_failure('not_found', response: actual, last_request: last_request) | ||
end | ||
end | ||
|
||
# Add better debuggability to be_unprocessable failures | ||
RSpec::Matchers.define :be_unprocessable do | ||
match(&:unprocessable?) | ||
|
||
failure_message do |actual| | ||
debug_text_for_failure('unprocessable', response: actual, last_request: last_request) | ||
end | ||
end | ||
|
||
# Add better debuggability to be_successful failures | ||
RSpec::Matchers.define :be_successful do | ||
match(&:successful?) | ||
|
||
failure_message do |actual| | ||
debug_text_for_failure('successful', response: actual, last_request: last_request) | ||
end | ||
end | ||
|
||
# Add better debuggability to be_ok failures | ||
RSpec::Matchers.define :be_ok do | ||
match(&:ok?) | ||
|
||
failure_message do |actual| | ||
debug_text_for_failure('ok', response: actual, last_request: last_request) | ||
end | ||
end | ||
|
||
def debug_text_for_failure(expected, response:, last_request:) | ||
debug_text = "expected response to be #{expected} but HTTP code was #{response.status}." | ||
debug_text += " Last request was #{last_request.request_method} to #{last_request.fullpath}" | ||
unless last_request.get? | ||
debug_text += " with body:\n" + last_request.body.read | ||
end | ||
debug_text += "\nResponse body was:\n" + response.body | ||
debug_text | ||
end |