module RSpec::SleepingKingStudios::Deferred::Dependencies
Mixin for declaring dependent methods for deferred example groups.
Each dependent method is expected to have a definition, either as a direct method definition (using the ‘def` keyword or `define_method`), or via a memoized helper (such as `let`).
When the deferred examples are included in an example group and that example group is run, a before(:context) hook will check for all of the declared dependencies of that example group. If any of the expected dependencies are not defined, the hook will raise an exception listing the missing methods, the deferred examples that expect that method, and the description provided.
@example
module RocketExamples include RSpec::SleepingKingStudios::Deferred::Provider deferred_examples 'should launch the rocket' do include RSpec::SleepingKingStudios::Deferred::Dependencies depends_on :rocket, 'an instance of Rocket where #launched? returns false' describe '#launch' do it 'should launch the rocket' do expect { rocket.launch }.to change(rocket, :launched?).to be true end end end end
Public Class Methods
Source
# File lib/rspec/sleeping_king_studios/deferred/dependencies.rb, line 88 def check_dependencies_for(example) missing = missing_dependencies_for(example) return if missing.empty? raise MissingDependenciesError, generate_missing_message(missing) end
Checks for missing dependent methods for the given example.
@param example [RSpec::Core::Example] the example to check.
@raise [MissingDependenciesError] if there are any dependencies missing
for the given example.
Private Class Methods
Source
# File lib/rspec/sleeping_king_studios/deferred/dependencies.rb, line 98 def all_dependencies_for(example) example .class .ancestors .select { |ancestor| ancestor.respond_to?(:dependent_methods) } .flat_map(&:dependent_methods) end
Source
# File lib/rspec/sleeping_king_studios/deferred/dependencies.rb, line 106 def generate_missing_message(missing) # rubocop:disable Metrics/MethodLength [ 'Unable to run specs with deferred example groups because the ' \ 'following methods are not defined in the examples:', *missing .group_by { |item| item[:deferred_group] } .map do |deferred_group, dependencies| message_for_group(deferred_group, dependencies) end, 'Please define the missing methods or :let helpers.' ] .join("\n\n") end
Source
# File lib/rspec/sleeping_king_studios/deferred/dependencies.rb, line 120 def message_for_group(deferred_group, dependencies) message = "Missing methods for #{deferred_group.description.inspect}:" dependencies.each do |hsh| message += "\n ##{hsh[:method_name]}" message += ": #{hsh[:description]}" if hsh[:description] end message end
Source
# File lib/rspec/sleeping_king_studios/deferred/dependencies.rb, line 131 def missing_dependencies_for(example) all_dependencies_for(example).reject do |hsh| example.respond_to?(hsh[:method_name]) end end