Search code examples
ruby-on-railsrspeccapybaracocoon-gem

Testing fields added dynamically by cocoon using rspec and capybara


I was wondering whether anybody tests fields that were dynamically added by cocoon?

It's a great little time saver but all of the fields that are added dynamically have really long numerics added to the ID and name. This means that I have to skip testing that requires more than one (set of) field(s) on the page.


Solution

  • Maybe using Capybara finders all, first and the selector input. Something like this:

    visit new_resource_path
    click_link "Add a Nested Resource"
    first("input[name='nested_resource[name]']").set("Nested Resource")
    click_button "submit"
    

    Or

    visit new_resource_path
    click_link "Add a Nested Resource"
    click_link "Add a Nested resource"
    all("input[name='nested_resource[name]']").each do |input|
      input.set("Nested Resource")
    end
    click_button "submit
    

    This is only an approach, I've never worked with cocoon. This is however, a form to test dynamic inputs.