用黄瓜和水豚测试分拣 [英] Test sorting with Cucumber and Capybara

查看:156
本文介绍了用黄瓜和水豚测试分拣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法来测试列表与黄瓜和水豚的排序。使用javascript在客户端执行排序。

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

我在想的东西是:

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


$ b b

很遗憾,我不知道如何建立步骤。

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

感谢您的帮助!

推荐答案

这是一个好主意,分离出你要测试的故事(你想接近纯英语)和测试的实际实现(它隐藏在step_definitions )。

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).

有几种方法可以解决这个问题,这取决于你想测试什么。在第一种情况下,cuke测试是非常可读的,它归结为正确实现步骤定义:

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

在这种情况下,你必须定义一个列表的含义(如果需要,可以在步骤def中将其分配给@list),然后按照排序顺序查看列表(这里你可以传递一个正则表达式,确保你在项目2之前看到项目1等)

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.)

或者,如果你喜欢在cuke测试中更冗长,做一些像:

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/

这假设列表已填充。

取决于列表的位置,您可能必须在范围内使用范围参数。

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

记住,黄瓜是伟大的功能和集成测试,但可能不是正确的工具,单元测试排序(看所有边缘情况)。要在单元测试级别测试排序,我强烈建议使用QUnit。因为QUnit测试是静态页面,所以尝试这个技巧来运行测试作为capybara的一部分:

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']"

这篇关于用黄瓜和水豚测试分拣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆