Q:

Error compiling ERB code from string

I'm writing some tests for my view helper (Rails 4.0) and trying to compile ERB code in a string that executes it. However, for the sake of simplicity, I am using a common method found in the rails form helpers here and receiving the same error:
Failure/Error: ERB.new(template).result
SyntaxError:
  (erb):1: syntax error, unexpected ')'
  ...out.concat(( field_set_tag do ).to_s); _erbout.concat "\n\t\...
  ...                               ^
  (erb):4: syntax error, unexpected end-of-input, expecting ')'
  ; _erbout.force_encoding(__ENCODING__)
                                        ^

This is the code I'm executing:
template = <<-TEMPLATE
<%= field_set_tag do %>
    Lorem Ipsum
<% end %>
TEMPLATE

ERB.new(template).result

I've found some suggestions to use the <% instead of <%= but this results in Lorem Ipsum being the only output. I've also tried using HAML instead of ERB but came out with similar results.
How can I get my template to output <fieldset>Lorem Ipsum</fieldset> using the field_set_for helper inside a string?

A:

“Normal” Eruby doesn’t allow for expressions using <%= to have blocks the way Rails uses them. Rails extends the Erubis Eruby handler to add support for them. Your test is just tring to use Erb directly, so this support isn’t available.
You need to make sure this support is loaded for this to work. I can get get it working with this:
ActionView::Template::Handlers::Erubis.new(template).evaluate(ActionView::Base.new)

I don’t know if this is the best way though. Check the RSpec docs, there may be a better way for testing parts of views like this.

