Class: Karafka::Core::Helpers::RSpecLocator

Inherits:
Module
  • Object
show all
Defined in:
lib/karafka/core/helpers/rspec_locator.rb

Overview

RSpec extension for the RSpec.describe subject class auto-discovery It automatically detects the class name that should be described in the given spec based on the spec file path.

Examples:

Just include it, extend with instantiation and use RSpec#describe_current

instead of `RSpec#describe`
RSpec.extend SupportEngine::RSpecLocator.new(__FILE__)

Instance Method Summary collapse

Constructor Details

#initialize(spec_helper_file_path, inflections = {}) ⇒ RSpecLocator

Returns a new instance of RSpecLocator.

Parameters:

  • spec_helper_file_path (String)

    path to the spec_helper.rb file

  • inflections (Hash<String, String>) (defaults to: {})

    optional inflections map



17
18
19
20
21
# File 'lib/karafka/core/helpers/rspec_locator.rb', line 17

def initialize(spec_helper_file_path, inflections = {})
  super()
  @inflections = inflections
  @specs_root_dir = ::File.dirname(spec_helper_file_path)
end

Instance Method Details

#extended(rspec) ⇒ Object

Builds needed API

Parameters:

  • rspec (Module)

    RSpec main module



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/karafka/core/helpers/rspec_locator.rb', line 25

def extended(rspec)
  super

  this = self

  # Allows "auto subject" definitions for the `#describe` method, as it will figure
  # out the proper class that we want to describe
  # @param block [Proc] block with specs
  rspec.define_singleton_method :describe_current do |&block|
    describe(this.inherited, &block)
  end
end

#inheritedClass

Returns class name for the RSpec #describe method.

Returns:

  • (Class)

    class name for the RSpec #describe method



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/karafka/core/helpers/rspec_locator.rb', line 39

def inherited
  caller(2..2)
    .first
    .split(':')
    .first
    .gsub(@specs_root_dir, '')
    .gsub('_spec.rb', '')
    .split('/')
    .delete_if(&:empty?)
    .itself[1..]
    .join('/')
    .camelize
    .then { |string| transform_inflections(string) }
    .constantize
end