I like writing test for helpers that output html, but the fact that assert_dom_equal
requires the whitespace to be the same always surprises me the first time I add a test to a new app. There are a few open issues and patches related to this but none seem to be getting reviewed or merged:
https://github.com/rails/rails-dom-testing/issues/62 https://github.com/rails/rails-dom-testing/pull/71 https://github.com/rails/rails-dom-testing/pull/66
I’ve put the following patch into my test_helper.rb
for the last few projects and it works well enough, but it would be nice to not copy and paste this into each app.
class ActionView::TestCase
private
# Used by assert_dom_select
# Patch strips newlines and space around them so assertion is less brittle
def fragment(text)
Nokogiri::HTML::DocumentFragment.parse(text.gsub(/[\s]*\n[\s]*/, ''))
end
end