方案概述是否支持变量?

问题描述 投票:1回答:1

我走过Scenario Outlines Wiki,但我有一些问题:

1,例如,如下图所示,数据表值是否支持变量?

Scenario Outline: eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers

  Examples:
    | start               | eat | left |
    |  <%= cb.prefix %>   |  5  |  7   |
    |  <%= cb.prefix %>   |  5  |  15  |

2,以下格式是否正确?或者我必须创建一个新变量来替换最后一个<left>?换句话说,如何重用这些变量,<start><eat><left>

Scenario Outline: eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers
  And there are <left> cucumbers left

  Examples:
    | start | eat | left | left |
    |  12   |  5  |  7   |  7   |
    |  20   |  5  |  15  |   7  |
cucumber
1个回答
0
投票

对于第一种情况,不幸的是,变量不是功能文件内部规范的一部分。

我发现,解决这个问题的一种方法是将您需要的信息存储在一个对象中,map:

starts = {prefix1: 12, prefix2: 20}

在步骤定义中使用它将意味着您可以执行此操作:

Given "there are $prefix cucumbers" do |prefix|
   prefix = starts[prefix]

   # Do stuff with the prefix
end

这样做是正确的意味着功能文件保持可读性,但您仍然可以使用它做更多。在过去,我以这种方式存储了URL,在地图中为它们创建了别名,并在我的步骤定义中使用了它。

在第二种情况下,您的第一种方法就是您可以采用的方法。

如果需要两次输入相同的参数,则示例表中的一列将用于输入该信息。但是,如果您要更改该列中的数据,那么您将使用新参数名称。

Scenario: I eat nothing
   Given I have <number_of_apples> apples
   When I eat 0 of them
   Then I should have <number_of_apples> left

Examples:
  | number_of_apples |
  | 5                |
  | 12               |

编辑

在评论中,问了一个问题“在一个场景中,由于其中一个步骤中的不同args,我必须重复许多步骤,那么,如何改进这个案例呢?”

在这种情况下,我将查看数据表功能。

功能文件中的示例如下所示:

Given I have an amount of the following fruits:
  | apples  |
  | oranges |
When I eat some apples
Then I should have a lesser amount of apples
But I should have the same amount of oranges

代码如下所示:

Given "I have an amount of the following fruits:" do |table|
   data = table.raw
   # do stuff with the data
end
© www.soinside.com 2019 - 2024. All rights reserved.