Faker will not code you. It will just give you a string like O'Malley
. But the answer should have HTML escaping (or some other kind, depending on the format), for example O'Malley
. You can always puts response.body
to verify this.
Compatibility RSpec matches
really designed for the regular expression expected
or actual
, but in your case both lines. Since the code has an optimization calling values_match?
which does a simple comparison , you effectively say expect(response.body).to eq(@thing.name)
.
If you need a regular expression, you are correct that you need to be careful using uncontrolled values ββto create it. Fortunately, Ruby has Regexp.escape
, so you can say Regexp.new("foo" + Regexp.escape(@thing.name) + "bar")
. But from your objection to include
, it looks like you really want the answer to only contain a name, right? In this case, you do not need a regular expression.
In any case, the problem is not what is around the name, but how the name is escaped. Therefore, before comparing, you must either (1) decode the response or (2) encode the faker string. It doesn't really matter. Both are pretty easy:
expect(CGI.unescapeHTML(response.body)).to eq @thing.name
or
expect(response.body).to eq CGI.escapeHTML(@thing.name)
Naturally, if your answer is JSON, you should replace all these HTML files with JSON, etc.
source share