我正在学习和喜欢黄瓜,现在有一个功能我不确定通过BDD继续进行的最佳方式:分页。我有一些场景(在我看来),其中有零页面,一页,几页等,并且我想确定某些页面上的某些记录,确保“下一步”按钮不是链接时最后一页,等等。
我将使用will_paginate,所以基本上我想弄清楚如何将BDD功能用于特定的项目列表,例如书籍。
我确信我可以糊涂它,但我觉得这应该是常见的,并希望看到别人做了什么。任何人都可以推荐一篇文章,或者给我一些示例代码,甚至可以自己拍一个例子吗?
答案 0 :(得分:19)
你可能可以使用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的链接状态,您可以添加一些后续点击页面链接场景,类似于下一页场景正在执行的操作(如果需要)。您可能还想添加额外的示例以检查您的分页是否正好为5,因为我选择的示例也会通过,如果分页是每页6。取决于您检查分页功能的确切目标。
如果您最终决定使用will_paginate以外的其他内容,那么您需要考虑更改的唯一内容可能是一些步骤(例如禁用的分页步骤)。
正如你提到要求链接一样,这也可能是一篇很好的博客文章;)