I am using Rails 4 with nested_attributes forms with the cocoon gem. This allows to add out of the box links to add a new element or remove it from the main class.

The problem is this gem when creating a new form does not give an unique identifier to the form.And I am trying to test it using Rspec, capybara and poltergeist.

Imagine the example I have an article that has many images and the respective views with the form to create it! here => https://gist.github.com/andreorvalho/5141c667a80be72edd5e and here => https://gist.github.com/andreorvalho/ca52e823a4930dac1a3c

The test fails with the following:

Failures:

  1) Article Admin creates a new article with more than one image
     Failure/Error: within_fieldset("image") do
     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching fieldset "image"
     # ./spec/features/article_spec.rb:90:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'

Obviously this happens because the id I gave them is the same and there is 2 forms and capybara cannot distinguish them, but does anybody know how I can do it?

Does cocoon have something implemented for this?

Do you know any other ways of testing it that would make sense?

Thanks in advance

有帮助吗?

解决方案

I end-up finding a work around:

within(all(:xpath, '//fieldset#image')[0]) do
  attach_file 'Image', test_image_path
  fill_in "Title", with: "My Article Image"
  fill_in "Description", with: "My Article Image Description"
  fill_in "Copyright", with: "My Article Image Copyright"
end

the all function returns an array with all the xpath for those requirments and then you can use it to iterate over it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top