使用 Cucumber 和 Capybara 进行测试排序

发布于 2024-10-06 18:39:08 字数 210 浏览 1 评论 0原文

有没有办法用黄瓜和水豚测试列表的排序。排序是在客户端使用 JavaScript 完成的。

我正在思考以下内容:

Then I should see "first element" and then I should see "second element"

不幸的是,我不知道如何构建步骤。

感谢您的帮助!

Is there a way to test sorting of a list with Cucumber and Capybara. The sorting is done client-side with javascript.

I was thinking something along the lines of:

Then I should see "first element" and then I should see "second element"

Unfortunately I have no idea how to approach building the steps.

Thanks for the help!

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

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

发布评论

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

评论(1

梦断已成空 2024-10-13 18:39:08

最好将您正在测试的故事(您希望接近简单的英语)和测试的实际实现(隐藏在 step_definitions 中)分开。

有几种方法可以解决这个问题,具体取决于您想要测试的内容。在第一种情况下,cuke 测试的可读性很强,归结为正确实现步骤定义:

Given that I am on page xyz
And I have a list
Then I should see the list in sorted order

在这种情况下,您必须定义拥有一个列表的含义(可以在步骤中将其分配给 @list) def 如果你愿意的话),然后按排序顺序查看列表意味着什么(在这里你可以传递一个正则表达式,确保你在第 2 项之前看到第 1 项,等等)。

或者,如果你喜欢在 cuke 测试中更详细,您可以执行类似以下操作:

Given that I am on page xyz
Then I should see /item1.*item2.*item3/

假设列表已填充。

根据列表的位置,您可能必须使用 within 范围参数。

请记住,黄瓜非常适合功能和集成测试,但可能不是进行单元测试的正确工具(考虑所有边缘情况)。要在单元测试级别测试排序,我强烈建议使用 QUnit。由于 QUnit 测试是静态页面,请尝试使用以下技巧将测试作为水豚的一部分运行:

Given I am on "/test/path/to/qunit/tests"
Then I should see "Whatever Title You have Assigned"
And I should see "0" within "//p[@id='qunit-testresult']/span/[@class='failed']"

It's a good idea to separate out the stories that you're testing (which you want to get close to plain English) and the actual implementation of the testing (which is hidden in the step_definitions).

There are a few ways to tackle this, depending on what you want to test. In the first case, the cuke test is very readable, and it boils down to implementing the step definitions correctly:

Given that I am on page xyz
And I have a list
Then I should see the list in sorted order

In this case, you'll have to define what it means to have a list (can assign it to @list in a step def if you want), and then what it means to see the list in sorted order (here you can pass a regex that ensures you see item 1 before item 2, etc.)

Alternatively, if you like being more verbose in the cuke tests, you can do something like like:

Given that I am on page xyz
Then I should see /item1.*item2.*item3/

which assumes the list is already populated.

Depending on where the list is, you may have to use a within scope param.

Remember that cucumber is great for functional and integration testing, but probably isn't the right tool for unit testing the sort (looking at all edge cases). To test the sorting at a unit test level, I'd highly recommend using QUnit. Since QUnit tests are static pages, try this trick for running the tests as part of capybara:

Given I am on "/test/path/to/qunit/tests"
Then I should see "Whatever Title You have Assigned"
And I should see "0" within "//p[@id='qunit-testresult']/span/[@class='failed']"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文