如何在单个示例中使用多个RSpec上下文?

问题描述 投票:0回答:1

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

ruby rspec
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.