提问者:小点点

cucumber场景大纲带数据表-找不到水豚元素


如何编写场景大纲来测试基于一个输出10个不同变量的变量的计算?

我尝试了各种选项并得到各种错误,包括:

Unable to find option "<frequency>" (Capybara::ElementNotFound)

(Cucumber::ArityMismatchError)

下面的代码给出了Capybara::ElementNotFind错误

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>

我是cucumber和水豚的新手,所以我不确定如何用数据表编写场景大纲。我做错了什么?


共2个答案

匿名用户

你做错的是试图在你的特征中写下关于你的计算是如何工作的细节。相反,你应该尝试用你的特征来解释你在做什么(它与频率有关,但除此之外我不知道)。当你采取这种方法时,你不必费心去指定场景中的实际结果,原因有很多

  1. 结果的值与这类测试没有太大关系。
  2. 将结果放入场景中是困难的,容易出错(拼写错误)并且极大地增加了维护成本

我将稍微解释一下第1点。

在这种情况下,您应该做的是推动您正在使用的更改频率功能的开发。这包括两部分

i)您有用户更改频率的UI,并且响应于此操作,您的UI显示频率更改的结果。

ii)当您更改频率时,会计算出正确的结果

第一部分,应该由cucumber中的一个场景驱动,所以你可以写这样的东西

给定…当我改变频率时,我应该会看到一组新的结果

第二部分不应该通过在Cucumber中编写场景来测试。相反,您应该为执行频率计算的事情编写单元测试。编写单元测试允许您

  • 编写更快的测试
  • 编写更多测试,以便处理边缘情况
  • 用编程语言编写测试,以便您可以轻松生成和使用任何类型的值

我看到Cucumber的新用户现在犯的最大错误是使用场景大纲和示例表。我建议你远离它们。每次你想使用一站式服务并思考一下。问问题

  1. 我在这里测试什么,为什么重要
  2. 我是否试图证明某些东西有效?如果是这样,我不应该使用单元测试来证明。

祝你好运:)

匿名用户

您的场景大纲应该只有一个示例表,并且您需要访问大纲中步骤中的“变量”。因此类似于以下内容(相应地更新了您的步骤定义)

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  |