使用 Cucumber 和 Capybara 进行测试排序
有没有办法用黄瓜和水豚测试列表的排序。排序是在客户端使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好将您正在测试的故事(您希望接近简单的英语)和测试的实际实现(隐藏在 step_definitions 中)分开。
有几种方法可以解决这个问题,具体取决于您想要测试的内容。在第一种情况下,cuke 测试的可读性很强,归结为正确实现步骤定义:
在这种情况下,您必须定义拥有一个列表的含义(可以在步骤中将其分配给 @list) def 如果你愿意的话),然后按排序顺序查看列表意味着什么(在这里你可以传递一个正则表达式,确保你在第 2 项之前看到第 1 项,等等)。
或者,如果你喜欢在 cuke 测试中更详细,您可以执行类似以下操作:
假设列表已填充。
根据列表的位置,您可能必须使用
within
范围参数。请记住,黄瓜非常适合功能和集成测试,但可能不是进行单元测试的正确工具(考虑所有边缘情况)。要在单元测试级别测试排序,我强烈建议使用 QUnit。由于 QUnit 测试是静态页面,请尝试使用以下技巧将测试作为水豚的一部分运行:
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:
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:
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: