Module: WideEvent::TestHelper
- Defined in:
- lib/wide_event/test_helper.rb
Overview
Minitest assertions for wide-event instrumentation. Require from your test helper and include in ActiveSupport::TestCase:
require "wide_event/test_helper"
class ActiveSupport::TestCase
include WideEvent::TestHelper
end
Instance Method Summary collapse
-
#assert_registered_wide_event_attributes ⇒ Object
Fails if strict mode saw any attribute that isn't declared in the registry.
-
#assert_wide_event(*keys, **pairs, &block) ⇒ Object
Runs the block inside an open wide event and asserts on the accumulated attributes.
-
#capture_wide_events(&block) ⇒ Object
Swaps the sink for an in-memory one for the duration of the block and returns every wide event flushed inside it.
Instance Method Details
#assert_registered_wide_event_attributes ⇒ Object
Fails if strict mode saw any attribute that isn't declared in the registry. Call from a dedicated test (or a teardown hook) with config.strict = true to make the registry an enforced schema.
48 49 50 51 52 53 |
# File 'lib/wide_event/test_helper.rb', line 48 def assert_registered_wide_event_attributes registry = WideEvent.config.registry violations = registry ? registry.violations : [] assert violations.empty?, "unregistered wide-event attributes #{violations.sort.inspect} - declare them in #{WideEvent.config.registry_path}" end |
#assert_wide_event(*keys, **pairs, &block) ⇒ Object
Runs the block inside an open wide event and asserts on the accumulated attributes. Bare string arguments assert presence; keyword-style pairs assert equality. Returns the attributes for further assertions.
assert_wide_event("pdf_render.duration_ms", "report.format" => "pdf") do
report.render
end
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/wide_event/test_helper.rb', line 18 def assert_wide_event(*keys, **pairs, &block) attrs = WideEvent.with do |store| block.call store.dup end keys.each do |key| assert attrs.key?(key), "expected wide event to have #{key.inspect}, got keys #{attrs.keys.inspect}" end pairs.each do |key, value| assert_equal value, attrs[key], "wide event attribute #{key.inspect}" end attrs end |
#capture_wide_events(&block) ⇒ Object
Swaps the sink for an in-memory one for the duration of the block and returns every wide event flushed inside it. Use around code that runs its own unit of work (jobs, full request dispatch).
35 36 37 38 39 40 41 42 43 |
# File 'lib/wide_event/test_helper.rb', line 35 def capture_wide_events(&block) original = WideEvent.config.sink sink = Sinks::Memory.new WideEvent.config.sink = sink block.call sink.events ensure WideEvent.config.sink = original end |