您可以在Cucumber的“给定",“何时"和“然后"步骤定义期间定义实例变量吗? [英] Can you define instance variables during Cucumber's Given, When, and Then step definitions

查看:103
本文介绍了您可以在Cucumber的“给定",“何时"和“然后"步骤定义期间定义实例变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用Cucumber,您可以在给定步骤定义期间定义实例变量. 该实例变量成为World范围的一部分. 然后,您可以在何时"和然后"的步骤定义期间访问此实例变量.

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.

是否可以在何时"和然后"步骤定义期间定义实例变量 并在以后的何时和然后"步骤定义中访问它们?

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

您将看到该场景通过了,这意味着该时间和随后步骤成功设置了实例变量.

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

事实上,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

请注意,方案中的给定"和何时"可以使用第一个"Then"步骤定义.

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

关于在何时何地然后设置步骤中设置实例变量是否是一种好习惯,这并不比在给定步骤中执行设置更糟.理想情况下,您的任何步骤都不会使用实例变量,因为它们会创建步骤耦合.但是,实际上,使用实例变量并没有遇到重大问题.

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的“给定",“何时"和“然后"步骤定义期间定义实例变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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