开发者

How would you write a Cucumber step editing a certain resource instance when there are many on the view?

开发者 https://www.devze.com 2023-04-06 03:12 出处:网络
Let\'s say you have a resource called Article. On the index page, you have many articles listed. In one scenario, I have this Cucumber step:

Let's say you have a resource called Article. On the index page, you have many articles listed. In one scenario, I have this Cucumber step:

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

I'm trying to identify that specific article - i.e. clicking the "edit" button for that specific article. I've been thinking about using the "within" helper of capybara, but I don't feel like that's the solution. I somehow need to find the name of the article on the page and th开发者_如何学JAVAen click that article's particular edit button... see what I mean?

If anyone has a clean solution, I'd appreciate knowing it. Thanks in advance.


What I do for this kind of thing is write a step like this:

When I edit article "Some article name or id"

My step definition then looks like this:

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

In my views I'm clearly defining the id on some parent element that will sufficiently scope calls like this. This pattern keeps you from having to get too dirty in the cukes themselves, while still giving you a certain degree of flexibility. For example:

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

精彩评论

暂无评论...
验证码 换一张
取 消