Module: Inspec::TestDslLazyLoader

Included in:
RSpec::Core::ExampleGroup
Defined in:
lib/inspec/rspec_extensions.rb

Overview

This module exists to intercept the method_missing instance method on RSpec::Core::ExampleGroup and is part of support for DSL plugintypes

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Support for test DSL plugins



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/inspec/rspec_extensions.rb', line 41

def method_missing(method_name, *arguments, &block)
  # Check to see if there is a test_dsl plugin activator hook with the method name
  registry = Inspec::Plugin::V2::Registry.instance
  hook = registry.find_activators(plugin_type: :test_dsl, activator_name: method_name).first

  if hook
    # OK, load the hook if it hasn't been already.  We'll then know a module,
    # which we can then inject into the context
    registry.activate(:test_dsl, method_name) unless hook.activated?

    # Inject the module's methods into the example group contexts.
    # implementation_class is the field name, but this is actually a module.
    # RSpec works by having these helper methods defined as instance methods.
    # So, we use include to inject the new DSL methods.
    RSpec::Core::ExampleGroup.include(hook.implementation_class)

    # We still haven't called the method we were looking for, so do so now.
    send(method_name, *arguments, &block)
  else
    super
  end
end