Method: Sentry::TestHelper#setup_sentry_test

Defined in:
lib/sentry/test_helper.rb

#setup_sentry_test {|config| ... } ⇒ void

This method returns an undefined value.

Alters the existing SDK configuration with test-suitable options. Mainly:

  • Sets a dummy DSN instead of ‘nil` or an actual DSN.

  • Sets the transport to DummyTransport, which allows easy access to the captured events.

  • Disables background worker.

  • Makes sure the SDK is enabled under the current environment (“test” in most cases).

It should be called before every test case.

Yield Parameters:

[View source]

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sentry/test_helper.rb', line 17

def setup_sentry_test(&block)
  raise "please make sure the SDK is initialized for testing" unless Sentry.initialized?
  dummy_config = Sentry.configuration.dup
  # configure dummy DSN, so the events will not be sent to the actual service
  dummy_config.dsn = DUMMY_DSN
  # set transport to DummyTransport, so we can easily intercept the captured events
  dummy_config.transport.transport_class = Sentry::DummyTransport
  # make sure SDK allows sending under the current environment
  dummy_config.enabled_environments += [dummy_config.environment] unless dummy_config.enabled_environments.include?(dummy_config.environment)
  # disble async event sending
  dummy_config.background_worker_threads = 0

  # user can overwrite some of the configs, with a few exceptions like:
  # - include_local_variables
  # - auto_session_tracking
  block&.call(dummy_config)

  # the base layer's client should already use the dummy config so nothing will be sent by accident
  base_client = Sentry::Client.new(dummy_config)
  Sentry.get_current_hub.bind_client(base_client)
  # create a new layer so mutations made to the testing scope or configuration could be simply popped later
  Sentry.get_current_hub.push_scope
  test_client = Sentry::Client.new(dummy_config.dup)
  Sentry.get_current_hub.bind_client(test_client)
end