2

I have overridden the standard Rails FormHelper to get it to spit form elements out in our (pretty much Bootstrap-based) format (after http://www.likeawritingdesk.com/posts/very-custom-form-builders-in-rails).

It works fine, but it is difficult to write a spec for. I am using a spec like this:

rendered_form = helper.my_custom_form_for(@account, :url => '/accounts') do |f|
  f.inputs do
    # This isn't rendered
    f.text_field 'name', :size => 30 
    # This is rendered
    f.select 'locale', options_for_select([%w(Australia en-AU), %w(UK en-GB)]) 
  end
end

What is rendered includes the select tag, but not the text field tag. I suspect this is because of the way ActionView handles blocks now, appending the return value of the block rather than the return value of each statement in the block. Obviously, when used in context in an app, the helper method gets passed some ERB and evaluates in that context.

  1. Am I right that it's not possible to test rendering helper methods in this way, with multiple statements in a block?
  2. If I am right, what would be the least hacky way of making a spec that does the same thing as the helper does in the context of a full app? Make an ERB string and somehow pass it to the helper to render?
Ben MacLeod
  • 128
  • 7

1 Answers1

1

Did you solve this? I am curious if you are just getting the last statment in the block returned. Have you tried changing the f.text_field/f.select order to see what gets returned? If so, then you just need to add them:

rendered_form = helper.my_custom_form_for(@account, :url => '/accounts') do |f|
  f.inputs do
    f.text_field('name', :size => 30) +
    f.select('locale', options_for_select([%w(Australia en-AU), %w(UK en-GB)]))
  end
end
noel
  • 2,095
  • 14
  • 14