Ruby 如何制作';在'之前;仅针对共享示例的块?

Ruby 如何制作';在'之前;仅针对共享示例的块?,ruby,rspec,Ruby,Rspec,考虑以下规范test.rb: describe 'Thing' do shared_examples 'becomes_sad' do before(:all) do puts 'Begin becomes_sad' end after(:all) do puts 'Finalize becomes_sad'

考虑以下规范
test.rb

describe 'Thing' do

    shared_examples 'becomes_sad' do

            before(:all) do
                    puts 'Begin becomes_sad'
            end

            after(:all) do
                    puts 'Finalize becomes_sad'
            end

            it 'shared test #1' do; end
            it 'shared test #2' do; end

    end

    shared_examples 'becomes_happy' do

            before(:all) do
                    puts 'Begin becomes_happy'
            end

            after(:all) do
                    puts 'Finalize becomes_happy'
            end

            it 'shared test #3' do; end
    end

    include_examples 'becomes_sad'
    include_examples 'becomes_happy'

end
当我运行
rspec--format documentation test.rb
时,我收到:

Thing
  Begin becomes_sad
  Begin becomes_happy
    shared test #1
    shared test #2
    shared test #3
  Finalize becomes_happy
  Finalize becomes_sad
我所期望和需要的是:

Thing
  Begin becomes_sad
    shared test #1
    shared test #2
  Finalize becomes_sad
  Begin becomes_happy
    shared test #3
  Finalize becomes_happy

我该怎么做?RSpec版本是2.99。

实际上,(:all)之前的两个
块都将添加到相同的示例组/上下文中。因为像
before
hooks这样的东西作为一个整体应用于示例组,如果您想要不同的行为,那么您需要创建不同的示例组

你必须做一些类似的事情

context 'sad' do
  include_examples 'becomes_sad'
end

context 'happy' do
  include_examples 'becomes_happy'
end