Skip to content

Commit 47dabe5

Browse files
committed
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
1 parent b9a156e commit 47dabe5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

spec/support/custom_matchers.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Add better debuggability to be_forbidden failures
2+
RSpec::Matchers.define :be_forbidden do
3+
match(&:forbidden?)
4+
5+
failure_message do |actual|
6+
"expected response to be forbidden but HTTP code was #{actual.status}." \
7+
" Response body was:\n" + actual.body
8+
end
9+
end
10+
11+
# Add better debuggability to be_not_found failures
12+
RSpec::Matchers.define :be_not_found do
13+
match(&:not_found?)
14+
15+
failure_message do |actual|
16+
"expected response to be not_found but HTTP code was #{actual.status}." \
17+
" Response body was:\n" + actual.body
18+
end
19+
end
20+
21+
# Add better debuggability to be_unprocessable failures
22+
RSpec::Matchers.define :be_unprocessable do
23+
match(&:unprocessable?)
24+
25+
failure_message do |actual|
26+
"expected response to be unprocessable but HTTP code was #{actual.status}." \
27+
" Response body was:\n" + actual.body
28+
end
29+
end
30+
31+
# Add better debuggability to be_successful failures
32+
RSpec::Matchers.define :be_successful do
33+
match(&:successful?)
34+
35+
failure_message do |actual|
36+
"expected response to be successful but HTTP code was #{actual.status}." \
37+
" Response body was:\n" + actual.body
38+
end
39+
end
40+
41+
# Add better debuggability to be_ok failures
42+
RSpec::Matchers.define :be_ok do
43+
match(&:ok?)
44+
45+
failure_message do |actual|
46+
"expected response to be ok but HTTP code was #{actual.status}." \
47+
" Response body was:\n" + actual.body
48+
end
49+
end

0 commit comments

Comments
 (0)