From bd24f18ed71091975af3cd2ca0c449615ba4aea4 Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Sun, 12 Jan 2020 11:44:37 +0200 Subject: [PATCH 1/2] Add better debuggability to be_X test failures This way test failures will show what the HTTP status code actually was when test fails as well as output the entire response body. The response body will most likely contain vital debugging information to help figure out why the test failed --- spec/support/custom_matchers.rb | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 spec/support/custom_matchers.rb diff --git a/spec/support/custom_matchers.rb b/spec/support/custom_matchers.rb new file mode 100644 index 00000000..00fa474a --- /dev/null +++ b/spec/support/custom_matchers.rb @@ -0,0 +1,49 @@ +# Add better debuggability to be_forbidden failures +RSpec::Matchers.define :be_forbidden do + match(&:forbidden?) + + failure_message do |actual| + "expected response to be forbidden but HTTP code was #{actual.status}." \ + " Response body was:\n" + actual.body + end +end + +# Add better debuggability to be_not_found failures +RSpec::Matchers.define :be_not_found do + match(&:not_found?) + + failure_message do |actual| + "expected response to be not_found but HTTP code was #{actual.status}." \ + " Response body was:\n" + actual.body + end +end + +# Add better debuggability to be_unprocessable failures +RSpec::Matchers.define :be_unprocessable do + match(&:unprocessable?) + + failure_message do |actual| + "expected response to be unprocessable but HTTP code was #{actual.status}." \ + " Response body was:\n" + actual.body + end +end + +# Add better debuggability to be_successful failures +RSpec::Matchers.define :be_successful do + match(&:successful?) + + failure_message do |actual| + "expected response to be successful but HTTP code was #{actual.status}." \ + " Response body was:\n" + actual.body + end +end + +# Add better debuggability to be_ok failures +RSpec::Matchers.define :be_ok do + match(&:ok?) + + failure_message do |actual| + "expected response to be ok but HTTP code was #{actual.status}." \ + " Response body was:\n" + actual.body + end +end From 9a1d0b0cea557bc289bba758fbb7b6550931ad55 Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Sun, 12 Jan 2020 12:00:30 +0200 Subject: [PATCH 2/2] Output last request information on spec failures --- spec/support/custom_matchers.rb | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/spec/support/custom_matchers.rb b/spec/support/custom_matchers.rb index 00fa474a..8a2f9681 100644 --- a/spec/support/custom_matchers.rb +++ b/spec/support/custom_matchers.rb @@ -3,8 +3,7 @@ match(&:forbidden?) failure_message do |actual| - "expected response to be forbidden but HTTP code was #{actual.status}." \ - " Response body was:\n" + actual.body + debug_text_for_failure('forbidden', response: actual, last_request: last_request) end end @@ -13,8 +12,7 @@ match(&:not_found?) failure_message do |actual| - "expected response to be not_found but HTTP code was #{actual.status}." \ - " Response body was:\n" + actual.body + debug_text_for_failure('not_found', response: actual, last_request: last_request) end end @@ -23,8 +21,7 @@ match(&:unprocessable?) failure_message do |actual| - "expected response to be unprocessable but HTTP code was #{actual.status}." \ - " Response body was:\n" + actual.body + debug_text_for_failure('unprocessable', response: actual, last_request: last_request) end end @@ -33,8 +30,7 @@ match(&:successful?) failure_message do |actual| - "expected response to be successful but HTTP code was #{actual.status}." \ - " Response body was:\n" + actual.body + debug_text_for_failure('successful', response: actual, last_request: last_request) end end @@ -43,7 +39,16 @@ match(&:ok?) failure_message do |actual| - "expected response to be ok but HTTP code was #{actual.status}." \ - " Response body was:\n" + actual.body + 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