代码之家  ›  专栏  ›  技术社区  ›  SingleShot

用黄瓜测试分页特性

  •  3
  • SingleShot  · 技术社区  · 15 年前

    我相信我能应付过去,但我觉得这应该很普遍,我想看看其他人都做了些什么。有谁能推荐一篇文章,或者给我指出一些示例代码,或者自己尝试一下示例?

    1 回复  |  直到 15 年前
        1
  •  19
  •   Jamie Macey    15 年前

    你也许可以用它逃过一劫 scenario outlines 在功能文件中减少重复,但要注意,它将扩展到大量实际运行的场景,因此速度会比预期的慢。假设每页5本书,这样的方法可能会奏效。我将把步骤定义留作练习,但它们应该非常简单。

    我还应该提到的是,我实际上还没有运行这个程序,所以对任何语法错误都要小心谨慎。

    Feature: Book Browsing Pagination
    
      Scenario: No results
        Given I have 0 books
        When I view all books
        Then I should see "No results" on the page
    
      Scenario: Some results
        Given I have 3 books
        When I view all books
        Then I should see "Book 1"
        And I should see "Book 2"
        And I should see "Book 3"
    
      Scenario: Page links
        Given I have <count> books
        When I view all books from page <page>
        Then I should see a link to page <target page>
    
        Examples:
          | count | page | target page |
          |   8   |   1  |       2     |
          |   8   |   2  |       1     |
          |  13   |   1  |       2     |
          |  13   |   1  |       3     |
          |  13   |   2  |       1     |
          |  13   |   2  |       3     |
          |  13   |   3  |       1     |
          |  13   |   3  |       2     |
    
      Scenario: Page links for current page
        Given I have <count> books
        When I view all books from page <page>
        Then I should see a disabled pagination link to page <page>
    
        Examples:
          | count | page |
          |   8   |  1   |
          |   8   |  2   |
          |  13   |  1   |
          |  13   |  2   |
          |  13   |  3   |
    
      Scenario: Next Page links
        Given I have <count> books
        When I view all books from page <page>
        Then I should see a link "Next Page"
    
        When I click "Next Page"
        Then I should be on page <next page> # assert against query params maybe?
        # alternately, to keep page requests down, one could use something like:
        # Then I should see a link "Next Page" going to "?p=<next page>"
    
        Examples:
          | count | page | next page |
          |   8   |   1  |      2    |
          |  13   |   1  |      2    |
          |  13   |   2  |      3    |
    
      Scenario: Next Page links on last page
        Given I have <count> books
        When I view all books from page <page>
        Then I should see a disabled pagination link "Next Page"
    
        Examples:
          | count | page |
          |   8   |   2  |
          |  13   |   3  |
    

    类似的场景可用于检查“上一页/第一页/最后一页”和“下一页”的链接状态,如果需要,您可以向页面链接场景添加一些与“下一页”场景类似的后续单击。您可能还需要添加额外的示例来检查分页是否正好为5,因为如果分页为每页6,我选择的示例也会通过。取决于检查分页功能的具体目标。

    如果您最终决定使用will_paginate以外的其他方法,那么您只需要考虑更改其中的几个步骤(如禁用的分页)。

    正如你提到的,这可能也是一篇很好的博客帖子;)