Rails 4 + Cucumber:在页面上找不到复选框元素?

时间:2015-04-17 17:46:18

标签: ruby-on-rails cucumber

Controller中的new方法中的代码:

...
@categories = Category.all
...

new.html.erb视图中的代码:

<% unless @categories.nil? %>
    <% @categories.each do |t| %>
        <%= t.name %>
        <%= check_box_tag "categories[#{t.id}]", t.name %>

        <br />
    <% end %>
<% end %>

呈现的HTML是:

  Entertainment
  <input id="categories_6" name="categories[6]" type="checkbox" value="Entertainment" />

  <br />
  Health
  <input id="categories_5" name="categories[5]" type="checkbox" value="Health" />

  <br />
  Politics
  <input id="categories_3" name="categories[3]" type="checkbox" value="Politics" />

  <br />
  Sports
  <input id="categories_8" name="categories[8]" type="checkbox" value="Sports" />

  <br />
  Tech
  <input id="categories_4" name="categories[4]" type="checkbox" value="Tech" />

  <br />
  Travel
  <input id="categories_7" name="categories[7]" type="checkbox" value="Travel" />

  <br />
  United States
  <input id="categories_1" name="categories[1]" type="checkbox" value="United States" />

  <br />
  World
  <input id="categories_2" name="categories[2]" type="checkbox" value="World" />

  <br />

在我的Cucumber测试中,我希望能够检查我列出的类别,因此我有以下情况/步骤:

步骤:

When /^(?:|I )check "([^"]*)"$/ do |field|
  check(field)
end

情景:

@javascript 
Scenario: a signed in user can post an article with an associated category
  Then I should see "Sign out"
  Then I should see "New Article"
  Then I should see "Hello"
  When I follow "New Article"
  And I fill in "Url" with "Hello_World_Article_URL.com"
  And I fill in "Initial comment" with "My first comment"
  And I check "categories_6"
  And I press "Create Article"
  Then I should see "Article created!"
  Then article url "Hello_World_Article_URL.com" should exist

然而,这在And I check "categories_6"步骤失败,报告了Capybara ElementNotFound错误。我不清楚为什么引用复选框&#34; id&#34;没有正常工作。有没有人能够深入了解我可能做错了什么或者如何解决这个问题?

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

When (/^(?:|I )check "([^"]*)"$/) do |field|
  check(field)
end