Module: TrackRelay::Testing

Defined in:
lib/track_relay/testing.rb,
lib/track_relay/testing/helpers.rb,
lib/track_relay/testing/minitest_assertions.rb

Overview

Opt-in testing entry point for TrackRelay.

‘lib/track_relay.rb` does NOT require this file — consumers add `require “track_relay/testing”` themselves in their `test_helper.rb` / `rails_helper.rb`. This keeps the auto setup/teardown helpers and the RSpec matcher hooks out of production runtime. The gem’s own ‘test/test_helper.rb` performs that require explicitly because the gem’s tests are themselves consumers of the testing surface.

‘test_mode!` swaps the configured subscriber list for a single Subscribers::Test for the duration of an example so consumer tests can assert against fired events without sending them to real adapters. `test_mode_off!` restores the previously captured list.

‘test_mode!` is idempotent: calling twice without restoring returns the same Test instance and does NOT clobber the originally-captured subscriber list. After `test_mode_off!`, calling `test_mode!` again creates a fresh Test subscriber so per-test buffers stay isolated.

Defined Under Namespace

Modules: Helpers, MinitestAssertions

Class Method Summary collapse

Class Method Details

.active?Boolean

Returns whether test_mode! is currently active.

Returns:

  • (Boolean)

    whether test_mode! is currently active



52
53
54
# File 'lib/track_relay/testing.rb', line 52

def active?
  !@test_subscriber.nil?
end

.test_mode!Subscribers::Test

Replace ‘TrackRelay.config.subscribers` with a single Subscribers::Test and snapshot the previous list. Idempotent.

Returns:



33
34
35
36
37
38
# File 'lib/track_relay/testing.rb', line 33

def test_mode!
  return @test_subscriber if active?
  test_subscriber = Subscribers::Test.new
  @previous_subscribers = TrackRelay.config.replace_subscribers([test_subscriber])
  @test_subscriber = test_subscriber
end

.test_mode_off!void

This method returns an undefined value.

Restore the subscriber list captured by test_mode!. No-op when not active.



44
45
46
47
48
49
# File 'lib/track_relay/testing.rb', line 44

def test_mode_off!
  return unless active?
  TrackRelay.config.replace_subscribers(@previous_subscribers)
  @previous_subscribers = nil
  @test_subscriber = nil
end

.test_subscriberSubscribers::Test?

Returns the active test subscriber, or nil.

Returns:



57
58
59
# File 'lib/track_relay/testing.rb', line 57

def test_subscriber
  @test_subscriber
end