我正在学习和喜欢 cucumber ,现在有一个功能我不确定通过 BDD 进行的最佳方式:分页。我有一些场景(在我看来)有零页、一页、几页等,我想确保某些记录在某些页面上,确保“下一步”按钮不是链接最后一页等
我将使用 will_paginate,所以基本上我想弄清楚如何为特定的项目列表(例如书籍)BDD 其功能。
我相信我可以解决它,但我觉得这应该很常见,并且想看看其他人做了什么。任何人都可以推荐一篇文章,或向我指出一些示例代码,甚至自己尝试一下示例吗?
最佳答案
您可能可以使用 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 |
类似的场景可用于检查 Previous/First/Last 和 Next 的链接状态,如果您愿意,您可以向 Page Links 场景添加一些类似于 Next Page 场景正在执行的操作的后续单击。您可能还想添加额外的示例来检查您的分页是否正好为 5,因为如果分页为每页 6,我选择的示例也会通过。取决于您检查分页功能的确切目标。
如果您最终决定使用 will_paginate 以外的其他内容,那么您唯一需要考虑更改的可能是一些步骤(例如禁用的分页)。
正如你提到的要求链接,这也可能是一篇很好的博客文章;)
关于ruby-on-rails - 使用 Cucumber 测试分页功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1541940/