Hello,
The problem: HTML special characters can cause =~
assertions to fail in LiveViewTest - Especially if used in conjunction with libraries that generate random data AKA Faker
Under the hood, all render_*
functions in Phoenix.LiveViewTest
eventually find their way back to the function Phoenix.LiveViewTest.render/1
which is essentially a wrapper around Floki.raw_html/2
raw_html/2
accepts a list of options (currently only 1 option): a boolean named :encode
. If :encode
is true (which it is by default), then things such as O'Connell
become encoded and rendered as O'Connell
in the test. This causes test assertions to fail intermittently.
What’s the current solution?
You can, in config/test.exs
, pass config :floki, :encode_raw_html, false
which will turn this off for all tests.
In the event we want to enable / disable this behavior for specific assertion(s), we are unable.
I would like to see the ability to do:
assert render(view, encode: false) =~ "O'Doyle"
assert render(view, encode: true) =~ "O'Doyle"
Currently, the only way I can think to do this is expand each of the render_*
functions with an optional args \\ []
argument but that seems cumbersome - and there are lots of render functions already.
I welcome all thoughts for or against the idea as well as ideas on how this might be better implemented
Thank you
You can read more about the problem described at Fix Your Flaky Faker Tests - John Elm Labs