【问题标题】:Request specs for nested_attributes form using cocoon使用 cocoon 请求嵌套属性表单的规范
【发布时间】:2014-02-08 15:28:55
【问题描述】:

我正在使用带有嵌套属性表单的 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种形式,capybara不能区分它们,但是有人知道我该怎么做吗?

cocoon 有为此实现的吗?

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

提前致谢

【问题讨论】:

  • 您找到解决方案了吗?也许@nathanvda 知道答案?
  • 我实际上找到了一个解决方法,并会尽快发布答案,但如果 cocoon 能提供一个帮手来测试它会很好!

标签: ruby-on-rails rspec capybara nested-forms cocoon-gem


【解决方案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,然后您可以使用它对其进行迭代。

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多