当视图中有很多资源实例时,您如何编写Cucumber步骤来编辑某个资源实例?

时间:2011-09-19 18:31:26

标签: ruby-on-rails ruby-on-rails-3 cucumber

假设您有一个名为Article的资源。在索引页面上,列出了许多文章。在一个场景中,我有这个Cucumber步骤:

When /^I activate the edit article switch for the article "(.+)"$/ do |name|

我正在尝试识别该特定文章 - 即点击该特定文章的“编辑”按钮 。我一直在考虑使用水豚的“内部”帮手,但我觉得这不是解决方案。我不知何故需要在页面上找到文章的名称,然后点击该文章的特定编辑按钮...看看我的意思?

如果有人有一个干净的解决方案,我很高兴知道它。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我为这类事做的是写下这样一个步骤:

When I edit article "Some article name or id"

我的步骤定义如下:

When %|I edit article "$article_name"| do |article_name|
  article = Article.find_by_name(article_name)
  within "#article_#{article.id}" do
    #do whatever you need to do for that article
    click "Edit"
  end
end

在我的观点中,我明确地在某个父元素上定义了id,这个元素将足够地调用这样的调用。这种模式可以让你不必过于肮脏自己,同时仍然给你一定程度的灵活性。例如:

<% @articles.each do |article| %>
  <div id="article_<%= article.id %>">
     Stuff in here <%= link_to "Edit", edit_article_path(article) %>
  </div>
<% end %>