Skip to content

Commit 5f668a1

Browse files
authored
Fix flakey tests by removing test data outliers (forem#22753)
1 parent a40a32d commit 5f668a1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

spec/support/faker_html_safe.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Prevent Faker from generating characters that get HTML-encoded in responses,
2+
# which causes string comparison failures in request specs.
3+
#
4+
# Characters like ' " & < > are HTML-escaped by Rails (e.g., O'Brien becomes
5+
# O&#39;Brien), so `expect(response.body).to include(name)` fails.
6+
#
7+
# This patches Faker::Base.translate — the lowest-level method that all Faker
8+
# generators use to pull strings from locale data — to strip HTML-unsafe
9+
# characters from all generated fake data.
10+
module FakerHtmlSafe
11+
HTML_UNSAFE_CHARS = "'\"&<>".freeze
12+
13+
def translate(*args, **opts)
14+
result = super
15+
sanitize_faker_value(result)
16+
end
17+
18+
private
19+
20+
def sanitize_faker_value(value)
21+
case value
22+
when String
23+
value.tr(HTML_UNSAFE_CHARS, "")
24+
when Array
25+
value.map { |v| sanitize_faker_value(v) }
26+
else
27+
value
28+
end
29+
end
30+
end
31+
32+
Faker::Base.singleton_class.prepend(FakerHtmlSafe)

0 commit comments

Comments
 (0)