Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Add before_register hook to RSpec World #2094

Merged
merged 1 commit into from
Oct 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,17 @@ def hooks
@hooks ||= HookCollections.new(self, FilterableItemRepository::QueryOptimized)
end

# Invokes block before defining an example group
def on_example_group_definition(&block)
on_example_group_definition_callbacks << block
end

# @api private
# Returns an array of blocks to call before defining an example group
def on_example_group_definition_callbacks
@on_example_group_definition_callbacks ||= []
end

private

def handle_suite_hook(args, collection, append_or_prepend, hook_type, block)
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/core/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def registered_example_group_files
#
# Register an example group.
def register(example_group)
@configuration.on_example_group_definition_callbacks.each { |block| block.call(example_group) }
example_groups << example_group
@example_group_counts_by_spec_file[example_group.metadata[:absolute_file_path]] += 1
example_group
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ module RSpec::Core
end
end

describe '#on_example_group_definition' do
let(:configuration) do
tmp_config = RSpec::Core::Configuration.new
tmp_config.on_example_group_definition do |example_group|
example_group.examples.first.metadata[:new_key] = :new_value
end
tmp_config
end
let(:world) { RSpec::Core::World.new(configuration) }

it 'successfully invokes the block' do
example_group = RSpec.describe("group") { it "example 1" do; end}
world.register(example_group)
example = world.example_groups.first.examples.first
expect(example.metadata[:new_key]).to eq(:new_value)
end
end

describe '#deprecation_stream' do
it 'defaults to standard error' do
expect($rspec_core_without_stderr_monkey_patch.deprecation_stream).to eq STDERR
Expand Down