修改黄瓜周围钩子中的过程中的实例变量
使用黄瓜时,我引用步骤定义中使用的实例变量,例如
Given /^I have an instance variable in my step$/ do
@person.should_not be_nil
end
使用 Before 钩子在我的 env.rb 中使用 getter,例如
class Person
def be_happy
puts "smiling"
end
end
person = Person.new
Before do
@person = person
end
到目前为止一切都很好......
但如果我想使用 around 钩子,我的理解是它产生一个过程,我可以从中调用步骤中的块,例如,
Around do |scenario, block|
@person = person
block.call
end
但这失败了,因为@person为零。这是因为@person是在创建proc时实例化的,因此无法修改。有办法做到这一点吗?
When using cucumber, I reference an instance variable used in a step definition e.g.
Given /^I have an instance variable in my step$/ do
@person.should_not be_nil
end
With a getter in my env.rb using the Before hook e.g.
class Person
def be_happy
puts "smiling"
end
end
person = Person.new
Before do
@person = person
end
All good so far ...
But if I want to use an Around hook instead, my understanding is that it yields a proc from which I can call the block that was in the step e.g.
Around do |scenario, block|
@person = person
block.call
end
But this fails, as @person is nil. Is this because @person is instantiated when the proc is created, and thus cannot be modified. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到了两种解决方案
使用世界
尝试使用 World,也许在 env.rb 中有类似这样的东西:
然后你的步骤 def 应该做一些更像这样的事情:
通过这种方法,你在每个场景中都会得到一个新的分支 Person,这可能是一件好事。
使用常量
如果您不希望每个场景都有一个全新的 Person,只需使用一个常量,也许像这样:
使用步骤 def ,如下所示:
Two solutions come to mind
Using a World
Try using World, perhaps with something like this in env.rb:
And your step def should then do something more like:
With this approach, you get a branch new Person every scenario, which is probably a good thing.
Using a Constant
In the event you don't want a brand new Person for every scenario, just use a constant, perhaps like so:
with step def like so: