Class: FastCov::FactoryBotTracker

Inherits:
AbstractTracker show all
Defined in:
lib/fast_cov/trackers/factory_bot_tracker.rb

Overview

Tracks FactoryBot factory definition files when factories are used.

Factory files are typically loaded at boot time, before coverage starts. This tracker intercepts FactoryBot.factories.find (called by create/build) to record the source file where each factory was defined.

Register via: coverage_map.use(FastCov::FactoryBotTracker)

Defined Under Namespace

Modules: RegistryPatch

Instance Attribute Summary

Attributes inherited from AbstractTracker

#coverage_map

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractTracker

#initialize, #on_record, #on_start, #on_stop, #record, record, reset, #root, #start, #stop

Constructor Details

This class inherits a constructor from FastCov::AbstractTracker

Class Method Details

.record_factory_files(factory) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fast_cov/trackers/factory_bot_tracker.rb', line 31

def record_factory_files(factory)
  return unless active

  definition = factory.definition
  declarations = definition.instance_variable_get(:@declarations)
  return unless declarations

  declarations.each do |decl|
    block = decl.instance_variable_get(:@block)
    next unless block.is_a?(Proc)

    location = block.source_location
    record(location&.first)
  end
end

Instance Method Details

#installObject



14
15
16
17
18
19
20
# File 'lib/fast_cov/trackers/factory_bot_tracker.rb', line 14

def install
  gem "factory_bot"

  return if ::FactoryBot.factories.singleton_class.ancestors.include?(RegistryPatch)

  ::FactoryBot.factories.singleton_class.prepend(RegistryPatch)
end