Module: Checkend::Testing

Defined in:
lib/checkend/testing.rb

Overview

Testing utilities for Checkend SDK.

Use this module in your test suite to capture and inspect notices without actually sending them to the server.

Examples:

RSpec setup

RSpec.configure do |config|
  config.before(:each) do
    Checkend::Testing.setup!
  end

  config.after(:each) do
    Checkend::Testing.teardown!
  end
end

Minitest setup

class ActiveSupport::TestCase
  setup do
    Checkend::Testing.setup!
  end

  teardown do
    Checkend::Testing.teardown!
  end
end

Asserting on captured notices

def test_error_is_reported
  begin
    raise StandardError, 'Test error'
  rescue => e
    Checkend.notify(e)
  end

  assert_equal 1, Checkend::Testing.notices.size
  assert_equal 'StandardError', Checkend::Testing.last_notice.error_class
end

Defined Under Namespace

Classes: FakeClient

Class Method Summary collapse

Class Method Details

.clear_notices! ⇒ void

This method returns an undefined value.

Clear all captured notices



112
113
114
# File 'lib/checkend/testing.rb', line 112

def clear_notices!
  @fake_client&.clear!
end

.first_notice ⇒ Notice?

Get the first captured notice

Returns:

  • (Notice, nil) —

    the first notice or nil if none



105
106
107
# File 'lib/checkend/testing.rb', line 105

def first_notice
  notices.first
end

.last_notice ⇒ Notice?

Get the last captured notice

Returns:

  • (Notice, nil) —

    the last notice or nil if none



98
99
100
# File 'lib/checkend/testing.rb', line 98

def last_notice
  notices.last
end

.notice_count ⇒ Integer

Get the number of captured notices

Returns:

  • (Integer)


126
127
128
# File 'lib/checkend/testing.rb', line 126

def notice_count
  notices.size
end

.notices ⇒ Array<Notice>

Get all captured notices

Returns:

  • (Array<Notice>) —

    array of captured notices



89
90
91
92
93
# File 'lib/checkend/testing.rb', line 89

def notices
  return [] unless @fake_client

  @fake_client.notices
end

.notices? ⇒ Boolean

Check if any notices were captured

Returns:

  • (Boolean)


119
120
121
# File 'lib/checkend/testing.rb', line 119

def notices?
  !notices.empty?
end

.setup! ⇒ void

This method returns an undefined value.

Set up test mode - disables async sending and captures notices



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/checkend/testing.rb', line 48

def setup!
  @original_async = Checkend.configuration.async
  @original_client = Checkend.instance_variable_get(:@client)

  # Disable async to make tests deterministic
  Checkend.configuration.async = false

  # Replace client with fake client
  @fake_client = FakeClient.new
  Checkend.instance_variable_set(:@client, @fake_client)

  # Ensure SDK is started
  Checkend.instance_variable_set(:@started, true)

  # Clear any existing context
  Checkend.clear!
end

.teardown! ⇒ void

This method returns an undefined value.

Tear down test mode - restores original configuration



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/checkend/testing.rb', line 69

def teardown!
  # Restore original settings
  Checkend.configuration.async = @original_async if defined?(@original_async)
  Checkend.instance_variable_set(:@client, @original_client) if defined?(@original_client)

  # Clear captured notices
  clear_notices!

  # Clear context
  Checkend.clear!

  # Remove instance variables
  remove_instance_variable(:@original_async) if defined?(@original_async)
  remove_instance_variable(:@original_client) if defined?(@original_client)
  remove_instance_variable(:@fake_client) if defined?(@fake_client)
end