1

我正在使用带有嵌套属性表单的 Rails 4 和茧宝石。这允许添加开箱即用的链接以添加新元素或将其从主类中删除。

问题是创建新表单时这个 gem 没有为表单提供唯一标识符。我正在尝试使用 Rspec、capybara 和 poltergeist 对其进行测试。

想象一下这个例子,我有一篇文章,其中包含许多图像和相应的视图以及创建它的表单!这里 => https://gist.github.com/andreorvalho/5141c667a80be72edd5e 和这里​​ => https://gist.github.com/andreorvalho/ca52e823a4930dac1a3c

测试失败并显示以下内容:

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>'

显然这是因为我给他们的 id 是相同的,并且有 2 种形式,水豚无法区分它们,但是有人知道我该怎么做吗?

茧是否为此实施了一些措施?

你知道任何其他有意义的测试方法吗?

提前致谢

4

1 回答 1

1

我最终找到了解决方法:

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

all 函数返回一个包含这些要求的所有 xpath 的数组,然后您可以使用它来迭代它。

于 2014-04-14T07:56:44.560 回答