【发布时间】:2020-03-04 07:08:23
【问题描述】:
我有一组带有给定before 子句的上下文,我想围绕各种示例进行包装。这是我的尝试:
# The "multi-context" wrapper
def with_foo_and_bar(&block)
before { p 'hello world' }
context 'foo' do
before { p 'baz' }
yield
end
context 'bar' do
before { p 'qux' }
yield
end
end
# The example
describe do
with_foo_and_bar do
it 'prints some stuff' do
# Example runs twice, but only 'hello world' is printed
end
end
end
在本规范中,我希望所有before 子句都运行并打印出“baz”和“qux”一次,“hello world”两次,但只打印“hello world”(两次,正如预期的那样) .我觉得 yield 忽略 before 块存在一些问题,但我不确定如何调整代码以获得我想要的。如有任何建议,我将不胜感激!
【问题讨论】: