我有一组带有给定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
块存在一些问题,但是我不确定如何调整代码以获取所需的内容。我将不胜感激任何建议!