保存黄瓜变量? [英] Preserve variable in cucumber?
问题描述
我想在给定/ Then / When子句中访问变量。如何保留变量,以便它们可以随处访问?
I want to access variables in difference Given/Then/When clauses. How to preserve variables so that they are accessible everywhere?
Given(#something) do
foo = 123 # I want to preserve foo
end
Then(#something) do
# how to access foo at this point???
end
推荐答案
,您需要使用实例或全局变量。
To share variables across step definitions, you need to use instance or global variables.
当需要跨步定义共享数据时,可以使用实例变量,但只能进行一次测试(即变量在每个场景之后清除)。实例变量以@开头。
Instance variables can be used when you need to share data across step definitions but only for the one test (ie the variables are cleared after each scenario). Instance variables start with a @.
Given(#something) do
@foo = 123
end
Then(#something) do
p @foo
#=> 123
end
如果要在所有方案中共享变量,全局变量,以$开头。
If you want to share a variable across all scenarios, you could use a global variable, which start with a $.
Given(#something) do
$foo = 123
end
Then(#something) do
p $foo
#=> 123
end
注意:通常建议不要在步骤/方案之间共享变量它创建耦合。
Note: It is usually recommended not to share variables between steps/scenarios as it creates coupling.
这篇关于保存黄瓜变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!