如何编写方案大纲来测试基于输出 10 个不同变量的变量的计算?
我尝试了各种选项并收到各种错误,包括:
Unable to find option "<frequency>" (Capybara::ElementNotFound)
和
(Cucumber::ArityMismatchError)
下面的代码给出了水豚::ElementNotFound 错误
Scenario Outline:
When I select "<frequency>" frequency
And I press recalculate
Then I should see total amount and percentages recalculated by <frequency> frequency on the results page
Examples:
| frequency |
| weekly |
| daily |
Examples:
| tax | sub-total | total |
| 38.25 | 114.74 | 191.24 |
| 3.19 | 9.56 | 15.94 |
步骤定义
When(/^I select "([^"]*)" frequency$/) do |frequency|
select "<frequency>", from: "frequency"
end
Then(/^I should see total amount and percentages recalculated by <frequency> frequency on the results page$/) do |table|
expect(results_page).to have_content("<tax>")
expect(results_page).to have_content("<sub_total>")
expect(results_page).to have_content("<total>")
end
表单标记
<form action="change_result_frequency" method="post">
<label for="frequency">Frequency</label>
<select name="frequency" id="frequency">
<option value="yearly">yearly</option>
<option value="monthly">monthly</option>
<option selected="selected" value="weekly">weekly</option>
<option value="daily">daily</option>
</select>
<input type="submit" name="commit" value="Recalculate">
</form>
我是黄瓜和水豚的新手,所以我不确定如何使用数据表编写场景大纲。我做错了什么?
您做错了什么,就是试图在功能中编写有关计算工作原理的详细信息。相反,你应该尝试做的是使用你的功能来解释你在做什么(它与频率有关,但除此之外我不知道)。采用此方法时,由于多种原因,无需费心指定方案中的实际结果
- 结果的值与此类测试无关。
- 将结果放在场景中是困难的,容易出错(拼写错误),并且大大增加了维护成本
我将进一步解释第 1 点。
在此方案中,您应该做的是推动您正在使用的更改频率功能的开发。这包括两部分
i) 您有一个供用户更改频率的 UI,并且为了响应此操作,您的 UI 显示频率更改的结果。
ii) 当您更改频率时,会计算出正确的结果
第一部分,应该由黄瓜中的场景驱动,所以你可以写一些类似的东西
Given ...
When I change the frequency
Then I should see a new set of results
第二部分不应该通过在黄瓜中编写场景来测试。相反,您应该为执行频率计算的东西编写单元测试。编写单元测试允许您
- 编写更快的测试
- 编写更多测试,以便处理边缘情况
- 使用编程语言编写测试,以便轻松生成和使用任何类型的值
我看到现在黄瓜的新用户犯的最大错误是使用场景大纲和示例表。我建议你远离他们。每次你想使用一站并思考一下。提出问题
- 我在这里测试什么以及为什么它很重要
- 我是想证明某些东西有效吗?如果是这样,我不应该使用单元测试来证明。
祝你好运:)
您的方案大纲应该只有一个示例表,并且您需要访问大纲中步骤中的"变量"。 所以像下面这样(相应地更新您的步骤定义)
Scenario Outline:
When I select "<frequency>" frequency
And I press recalculate
Then I should see <tax>, <sub-total>, and <total> recalculated on the results page
Examples:
| frequency | tax | sub-total | total |
| weekly | 38.25 | 114.74 | 191.24 |
| daily | 3.19 | 9.56 | 15.94 |