Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 表中元素的测试顺序_Ruby On Rails_Ruby_Cucumber_Bdd - Fatal编程技术网

Ruby on rails 表中元素的测试顺序

Ruby on rails 表中元素的测试顺序,ruby-on-rails,ruby,cucumber,bdd,Ruby On Rails,Ruby,Cucumber,Bdd,我想运行一个测试来测试元素的顺序 我希望订单按日期升序 <div id="feeds"> <table> <caption><%= t('events.all') %></caption> <thead> <tr> <th><%= Event.t(:title) %></th> <th

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

<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) %>
这是我的黄瓜功能场景和最后一句的步骤

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
我这里真的有两个问题。第一个问题是如何正确指定测试?我可以指定我的测试以上不同和更正确

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

下面是我要测试的集合的代码。在某种程度上,我想检查下面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) %>

'事件',:集合=>@events%>

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


我可能会建议使用一个表来整合您的步骤

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.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)
event\u names.index(第一个示例)。不应为\u nil
事件名称。索引(第二个示例)。不应为空
事件名称.index(第一个示例).should<事件名称.index(第二个示例)

为什么不直接使用第n个子选择器

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

我们需要更多解释真正的问题同意@shingara,你想知道什么?另外,您的场景太长了。您可能应该重构该测试,因为您在一个场景中测试了很多不同的东西。稍后我想检查我的注册事件是否已排序,最新事件是否在顶部。这就是我想和桌子比较的东西。问题是测试只检查帐户是否有事件,而不是事件的顺序。我对黄瓜测试很陌生,所以我真的不知道一个好方法来指定我的测试。请回答您的问题,最好在这里回答。我还编辑了我的问题。然后如何断言事件在数组中的顺序?添加断言以确保它们在数组中并且顺序正确。这样做会导致错误混乱:nil:NilClass(NoMethodError)的未定义方法“body”:你在黄瓜后面用水豚还是网鼠?如果水豚你会使用“page”而不是“response”,你可能需要也可能不需要调用body方法。如果webrat可以正常工作,除非你还没有真正访问过一个页面。page.body.should是你需要的水豚。真棒。这正是我要找的。谢谢
page.should have_selector('tbody tr:nth-child(1)', text: first_example)
page.should have_selector('tbody tr:nth-child(2)', text: second_example)