黄瓜步骤定义中的实例变量(即自我)在玩什么对象? [英] What object is in play for instance variables (i.e. what is self) in Cucumber step definitions?

查看:87
本文介绍了黄瓜步骤定义中的实例变量(即自我)在玩什么对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不了解在Ruby中使用Cucumber时的作用域,尤其是在实例变量方面。

I'm not understanding scoping when using Cucumber in Ruby, especially with regard to instance variables.

对于我的直接示例,在的Before部分中 hooks.rb 分配了变量 @browser

For the context of my immediate example, in the Before portion of hooks.rb the variable @browser is assigned.

@browser = Watir :: Browser.new @ browser_selected.to_sym

(其中 @browser_selected 通常是'chrome')

(where @browser_selected is usually 'chrome')

在步骤定义中使用@browser。举一个简单的例子: @ browser.send_keys(:tab)

In step definitions @browser gets used. As a simple example: @browser.send_keys(:tab)

我不明白的是什么对象包含@browser作为属性。在这种情况下它有什么意义?我知道我困惑的代码始终在一个块中,并且我知道每个这样的块(通过附加到它的Given / When / Then消息)都以某种神秘的方式进行了预处理。

What I don't understand is what object contains @browser as an attribute. How does it have meaning in this context? I know the code I'm puzzled by is always in a block, and I recognize that each such block is used (via the Given/When/Then message to which it is attached) to be pre-processed in some mysterious way.

在这个神秘的事物之中,是实例变量的作用域。我怎么知道此类块中实例变量的范围?

Amidst this shrouding in mystery is the scoping of instance variables. How can I know the scope of instances variables within such blocks?

推荐答案

self 只是一个Ruby对象,即世界,在每种情况下都使用它。每个步骤定义中的块都是在世界范围内使用 the_world.instance_eval 或类似的东西执行的,这意味着当每个块运行 self 是世界。因此,所有这些实例变量所属的对象是同一个对象,即世界。所有这些实例变量的范围都是整个场景。

self in Cucumber steps and hooks is just a Ruby object, the "world", which is used throughout each scenario. The block in each step definition is executed in the context of the world with the_world.instance_eval or something similar, meaning that when each block runs self is the world. So the object to which all of those instance variables belong is the same object, the world. The scope of all of those instance variables is the entire scenario.

因此,在Cucumber步骤中谨慎使用实例变量并在步骤名称中明确指出您很重要正在使用它们(也就是说,在步骤名称中明确指出它们是指某种状态)。这些步骤显然是指保存在幕后的事物(即,引用相同的实例变量):

So it's important to use instance variables sparingly in Cucumber steps, and to make it clear in step names that you're using them (that is, make it clear in step names that they refer to some piece of state). These steps clearly refer to a thing which is saved behind the scenes (i.e. refer to the same instance variable):

Given there is a thing
When I frob the thing
Then the thing should be frobbed

很好,很正常。但是如果当我弄乱事物时,这将是可怕的预计算一些预期的断言结果并将它们也存储在实例变量中,然后然后应该弄乱事物在其断言中使用了这些实例变量。 然后应该把东西弄乱,除非当我把东西弄乱之前,它不能再使用,并且这种限制对于其他编写功能的人来说并不明显,并且会使他们感到沮丧。 (不要像我的前同事一样。)

That's fine and normal. But it would be terrible if When I frob the thing precalculated some expected assertion results and stashed them in instance variables too, and Then the thing should be frobbed used those instance variables in its assertions. Then the thing should be frobbed would not work unless When I frob the thing preceded it, making it less reusable, and that restriction would not be obvious to others writing features and they would become frustrated. (Don't be like my former colleague.)

回到世界:黄瓜为每种情况创造了一个新世界,并在最后将其扔掉,所以一个情况的实例变量不会影响下一个场景。在普通的黄瓜中,世界只是 Object 的一个实例。在Cucumber-rails中,它是 Cucumber :: Rails :: World 这很有趣)。除了内置在黄瓜导轨中的方法之外,世界还通过扩展模块来获得其方法(如 the_world.extend SomeModule )。与实例变量一样,世界扩展的所有模块中的所有方法都卡在同一对象(世界)上,因此有时您需要担心名称冲突。

Back to the world: Cucumber makes a new world for each scenario and throws it away at the end so a scenario's instance variables don't affect the next scenario. In plain Cucumber, the world is just an instance of Object. In cucumber-rails, it's an instance of Cucumber::Rails::World (which is interesting to read). Aside from the methods built in to the world in cucumber-rails, the world gets its methods by extending modules (as in the_world.extend SomeModule). As with instance variables, all the methods from all the modules that the world extends are jammed on to the same object (the world), so you sometimes need to worry about name conflicts.

这篇关于黄瓜步骤定义中的实例变量(即自我)在玩什么对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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