Skip to content

on_example_group_definition_callbacks support for example groups created via shared example groups

Created by: jmalves

Hey all,

I was trying to use on_example_group_definition_callbacks for example groups defined via shared examples however this does not seem to work. Example groups defined via shared examples do not run the callbacks.

For example if have something like:

describe 'Alphabet' do

  shared_examples_for 'a letter' do
    it 'is printable' do
      expect { p letter }.to_not raise_exception
    end
  end

  context 'when A' do
    it_behaves_like 'a letter'
  end
end

When doing:

RSpec.configure do |c|
  c.on_example_group_definition_callbacks do |group|
    ... # BehavesLikeAletter will never end up here
  end
end

Like it says in the comment the behaves like a letter example group created due to the shared example would never appear as a group.

My reasoning for the cause of this problem is that RSpec.world.record(group) is never run for these kinds of groups because they are not defined via an example group method but instead via a nested shared group method.

I am not sure if this behaviour is actually intended. I attempt to fix this in this PR. I am not confident if the approach I took is sound. Please let me know.

Merge request reports