黄瓜测试顺序的元素在表中

时间:2010-11-09 10:40:57

标签: ruby-on-rails ruby cucumber bdd

我想运行一个测试元素顺序的测试 我希望订单按日期升序。

这是我的黄瓜特征情景和最后一句的步骤。

Scenario: Order of the events should be Ascending
    Given I am signed into an account called "Gorilla Tech" as a customer
    When I follow "Register"
    And I follow "Events"
    And I follow "Register new event"
    Then I should see the header "Use the form below to register a new event"
    And I fill in "Title" with "Apefest 2010"
    And I fill in "Event at" with "2010-01-07"
    And I fill in "Add a note" with "This was truly awesome"
    Then I press "Create"
    Then I follow "Register new event"
    And I fill in "Title" with "Monkeyfest 2010"
    And I fill in "Event at" with "2010-01-08"
    And I fill in "Add a note" with "Yeah"
    Then "Apefest 2010" should appear before "Monkeyfest 2010"



   Then /"(.*)" should appear before "(.*)"/ do |first_example, second_example|
     response.body.should =~ /#{first_example}.*#{second_example}/
   end

我这里真的有两个问题。第一个是如何正确指定我的测试?我可以将上面的测试指定为不同且更正确吗?

我想要做的是在2个不同的日期注册2个不同的活动。该事件稍后将显示为网页上的列表。然后我想检查事件是否按日期排序。我希望最新日期的活动显示在顶部。

这是我要测试的集合的代码。在某种程度上,我想检查下面div中的集合是否按日期升序。

<div id="feeds">
    <table>
      <caption><%= t('events.all') %></caption>
      <thead>
        <tr>
          <th><%= Event.t(:title) %></th>
          <th><%= Event.t(:event_at) %></th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <%= render :partial => 'event', :collection => @events %>
      </tbody>
    </table>
  </div>

  <%= will_paginate(@events) %>

3 个答案:

答案 0 :(得分:13)

您编写步骤定义的方法很好,您只需要将多行(m)开关添加到您的模式中。 :)

response.body.should =~ /#{first_item}.*#{second_item}/m

答案 1 :(得分:4)

我可能会建议使用表格对您的步骤进行一些合并。

Scenario: Order of the events should be Ascending
...
And I fill in "Title" with "Apefest 2010"
And I fill in "Event at" with "2010-01-07"
And I fill in "Add a note" with "This was truly awesome"
Then I press create

我建议:

Scenario: Order of the events should be Ascending
...
When I register a new event:
| Title | Event at | Add a note |
| Apefest 2010 | 2010-01-07 | This was truly awesome |

您当前的解决方案有一个不太精确的权衡,查看整个响应正文,您可以从另一个位置的一个示例中找到文本。我建议使用XPATH达到你的疼痛阈值或使用,我假设你使用Watir(或变体),类。

您可以通过迭代行来收集表中的所有事件名称(并指定包含事件名称的列;这里我假设第一个)...

event_names = $browser.div(:id,"feeds").tables.first.rows.collect {|row| row[0].text}

然后声明事件存在并且它们在数组中以所需的顺序。

event_names.index(first_example).should_not be_nil
event_names.index(second_example).should_not be_nil
event_names.index(first_example).should < event_names.index(second_example)

答案 2 :(得分:0)

为什么不使用nth-child选择器?

page.should have_selector('tbody tr:nth-child(1)', text: first_example)
page.should have_selector('tbody tr:nth-child(2)', text: second_example)