x-govuk/govuk-form-builder

Investigate making the tests more realistic

Closed this issue · 1 comments

A recent bug #178 that caused content to be rendered multiple times should have been caught in tests but wasn't. It happens when using a templating layer, like in Rails, but not when using the library directly via Ruby.

It might be worth creating a smaller set of tests that render template files using Slim or ERB and ensure the content is as it seems.

Seemingly as usual with Rails, nothing's straightforward 😞

As outlined in Slim's README, Rails needs to capture blocks and Slim/Tilt alone don't.

module Helpers
  def headline(&block)
    if defined?(::Rails)
      # In Rails we have to use capture!
      "<h1>#{capture(&block)}</h1>"
    else
      # If we are using Slim without a framework (Plain Tilt),
      # this works directly.
      "<h1>#{yield}</h1>"
    end
  end
end

Without pulling a Rails app into the testing process I don't see a easy way to fix this. The other options include

  • a separate Rails app that can be used for testing
  • a dummy Rails app in spec/

I'm against this for a variety of reasons, namely that

  • it's easy to guard against by convention and,
  • the bugs, which usually manifest as repeated rendering of the element should be picked up by the test suite of any projects that use this library
  • finally the 1100 tests run on my machine in ~1.5 seconds, I'm keen not to slow them down