使用 Cucumber 测试分页功能

发布于 2024-08-07 17:01:09 字数 263 浏览 2 评论 0原文

我正在学习并喜欢 Cucumber,现在有一个功能,我不确定通过 BDD 继续进行的最佳方式:分页。我有一些场景(在我的脑海中),其中有零页、一页、几页等,并且我想确保某些记录位于某些页面上,请确保“下一页”按钮不是链接 我将使用 will_paginate,所以本质上

我想弄清楚如何 BDD 其功能用于特定的项目列表,例如书籍。

我确信我可以应付过去,但我觉得这应该很常见,并且想看看其他人做了什么。任何人都可以推荐一篇文章,或者向我指出一些示例代码,甚至自己尝试一个示例吗?

I am learning and liking cucumber, and now have a feature I'm not sure of the best way to proceed on via BDD: pagination. I have scenarios (in my mind) where there are zero pages, one page, several pages, etc. and where I want to make sure certain records are on certain pages, make sure the "next" button is not a link when on the last page, etc.

I will be using will_paginate, so essentially I want to figure out how to BDD its features for a specific list of items, for example, books.

I'm sure I can muddle through it but I feel this should be common and would like to see what others have done. Can anyone recommend an article, or point me to some example code, or even take a shot at an example themselves?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2024-08-14 17:01:09

您可能可以使用场景大纲来减少功能中的重复文件,但请注意,它将扩展到大量实际运行的场景,因此它会比您预期的慢。假设每页有 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 以外的其他内容,那么您需要考虑更改的唯一内容可能是一些步骤(例如禁用的分页步骤)。

正如您提到要求链接,这也可能是一篇很好的博客文章;)

You could probably get away with using scenario outlines to keep the repetition down in your feature file, but be aware that it will expand to a very large number of actually-run scenarios, so it'll be slower than you'd expect. Something like this should probably work, assuming 5 books per page. I'll leave the step definitions as an exercise, but they should be pretty straightforward.

I should also mention that I haven't actually run this, so take any syntax errors with a grain of salt.

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  |

Similar scenarios could be used for checking the link state of Previous/First/Last as for Next, and you could add some followup clicking to the Page Links scenario similar to what the Next Page scenario is doing if you wanted. You might also want to add extra examples to check that your pagination is at exactly 5, since the examples I've picked would also pass if pagination was 6 per page. Depends on what exactly your goals are for checking the pagination functionality.

If you eventually decide on something other than will_paginate, then the only things you'd need to look at changing might be a few of the steps (like the disabled pagination ones).

And as you mention asking for links, this might make a good blog post too ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文