您能否在 Cucumber 的 Given、When 和 Then 步骤定义期间定义实例变量 [英] Can you define instance variables during Cucumber's Given, When, and Then step definitions

查看:24
本文介绍了您能否在 Cucumber 的 Given、When 和 Then 步骤定义期间定义实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用 Cucumber,您可以在给定步骤定义期间定义实例变量.这个实例变量成为 World 范围的一部分.然后你可以在When和Then的步骤定义中访问这个实例变量.

I know with Cucumber, you can define instance variables during a Given step definition. This instance variable becomes part of the World scope. Then you can access this instance variable during step definitions of When and Then.

您能否在时间"和然后"步骤定义期间也定义实例变量并在后面的 When 和 Then 步骤定义中访问它们?

Can you define instance variables also during When and Then step definitions and access them in the later When and Then step definitions?

如果可能的话,定义实例变量是否是一种常见的做法?何时和然后步骤定义?

If it's possible, is it even a common practice to define instance variables during When and Then step definitions?

谢谢.

推荐答案

是的,您可以在任何步骤类型期间设置实例变量.

Yes, you can set instance variables during any step type.

例如,给定特征:

Feature: Instance variables

Scenario: Set instance variables during all steps
    Given a given step sets the instance variable to "1"
    Then the instance variable should be "1"
    When a when step sets the instance variable to "2"
    Then the instance variable should be "2"
    Then a then step sets the instance variable to "3"
    Then the instance variable should be "3"

以及步骤定义:

Given /a given step sets the instance variable to "(.*)"/ do |value|
    @your_variable = value
end
When /a when step sets the instance variable to "(.*)"/ do |value|
    @your_variable = value
end
Then /a then step sets the instance variable to "(.*)"/ do |value|
    @your_variable = value
end
Then /the instance variable should be "(.*)"/ do |value|
    @your_variable.should == value
end

您将看到场景通过,这意味着 when 和 then 步骤已成功设置实例变量.

You will see that the scenario passes, which means that the when and then steps were successfully setting the instance variable.

事实上,Given、When 和 Then 只是彼此的别名.仅仅因为您将步骤定义定义为Given",它仍然可以称为When"或Then".例如,如果使用的步骤定义为:

In fact, the Given, When and Then are just aliases of each other. Just because you defined a step definition as a "Given", it can still be called as a "When" or "Then". For example, the above scenario will still pass if the step definitions used were:

Then /a (w+) step sets the instance variable to "(.*)"/ do |type, value|
    @your_variable = value
end
Then /the instance variable should be "(.*)"/ do |value|
    @your_variable.should == value
end

请注意,场景中的Given"和When"可以使用第一个Then"步骤定义.

Notice that the first "Then" step definition can be used by the "Given" and "When" in the scenario.

至于在 when 和 then 步骤中设置实例变量是否是好的做法,这并不比在给定的步骤中进行.理想情况下,您的任何步骤都不会使用实例变量,因为它们会创建步骤耦合.但是,实际上,我在使用实例变量时并没有遇到重大问题.

As to whether it is good practice to set instance variables in when and then steps, it is no worse than doing it in given steps. Ideally, none of your steps would use instance variables as they create step coupling. But, practically speaking, I have not run into significant issues by using the instance variables.

这篇关于您能否在 Cucumber 的 Given、When 和 Then 步骤定义期间定义实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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