File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 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'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 )
You can’t perform that action at this time.
0 commit comments