Class: RSpecTelemetry::Subscribers::FactoryBot

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_telemetry/subscribers/factory_bot.rb

Constant Summary collapse

STACK_KEY =
:rspec_telemetry_fb_stack

Instance Method Summary collapse

Constructor Details

#initialize(recorder) ⇒ FactoryBot

Returns a new instance of FactoryBot.



17
18
19
20
# File 'lib/rspec_telemetry/subscribers/factory_bot.rb', line 17

def initialize(recorder)
  @recorder = recorder
  @subscription = nil
end

Instance Method Details

#finish(_name, _id, payload) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rspec_telemetry/subscribers/factory_bot.rb', line 41

def finish(_name, _id, payload)
  RSpecTelemetry.safely("factory_bot#finish") do
    frame = stack.pop
    next unless frame
    next unless @recorder.config.enabled && @recorder.config.capture_factory_bot

    now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    total = ((now - frame[:monotonic]) * 1000.0).round(3)
    self_ms = (total - frame[:child_ms]).round(3)

    parent = stack.last
    parent[:child_ms] += total if parent

    @recorder.record(
      "factory_bot.run_factory",
      factory: payload[:name].to_s,
      strategy: payload[:strategy].to_s,
      traits: Array(payload[:traits]).map(&:to_s),
      overrides: override_names(payload[:overrides]),
      duration_ms: total,
      self_duration_ms: self_ms,
      depth: stack.size,
      parent_factory: parent && parent[:name],
      factory_class: build_class_name(payload[:factory])
    )
  end
end

#start(_name, _id, payload) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rspec_telemetry/subscribers/factory_bot.rb', line 31

def start(_name, _id, payload)
  RSpecTelemetry.safely("factory_bot#start") do
    stack.push(
      name: payload[:name].to_s,
      monotonic: Process.clock_gettime(Process::CLOCK_MONOTONIC),
      child_ms: 0.0
    )
  end
end

#subscribeObject



22
23
24
# File 'lib/rspec_telemetry/subscribers/factory_bot.rb', line 22

def subscribe
  @subscription = ActiveSupport::Notifications.subscribe("factory_bot.run_factory", self)
end

#unsubscribeObject



26
27
28
29
# File 'lib/rspec_telemetry/subscribers/factory_bot.rb', line 26

def unsubscribe
  ActiveSupport::Notifications.unsubscribe(@subscription) if @subscription
  @subscription = nil
end