Module: Cased::TestHelper

Defined in:
lib/cased/test_helper.rb

Instance Method Summary collapse

Instance Method Details

#after_teardownObject



18
19
20
21
22
# File 'lib/cased/test_helper.rb', line 18

def after_teardown
  super

  Cased.publishers = @original_cased_publishers
end

#assert_cased_events(expected_event_count, expected_event_body = nil, &block) ⇒ void

This method returns an undefined value.

Assertion that helps with testing that a number of events have been published to Cased.

Examples:

Expected events with a filter inside of a block

def test_creates_user_create_event
  assert_cased_events 1, action: 'user.create' do
    create(:user)
  end
end

Expected events without a filter inside of a block

def test_creates_user_create_event
  assert_cased_events 1 do
    create(:user)
  end
end

Expected events with a filter for the duration of the test

def test_creates_user_create_event
  create(:user)

  assert_cased_events 1, action: 'user.create'
end

Expected events without a filter for the duration of the test

def test_creates_user_create_event
  create(:user)

  assert_cased_events 1
end

Cased::Model value hash

def test_creates_user_create_event
  user = create(:user)

  assert_cased_events 1, action: 'user.login', user: user
end

Parameters:

  • expected_event_count (Integer)

    The number of expected Cased events to be published.

  • expected_event_body (Hash) (defaults to: nil)

    Expected event to be published to Cased.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cased/test_helper.rb', line 78

def assert_cased_events(expected_event_count, expected_event_body = nil, &block)
  expected_event_body&.deep_symbolize_keys!

  actual_event_count = if block
    events_before_block = cased_events_with(expected_event_body)

    block&.call

    events_after_block = cased_events_with(expected_event_body)

    events_after_block.length - events_before_block.length
  else
    cased_events_with(expected_event_body).length
  end

  assert_equal expected_event_count, actual_event_count, "#{expected_event_count} Cased published events expected, but #{actual_event_count} were published"
end

#assert_no_cased_events(expected_event_body = nil, &block) ⇒ void

This method returns an undefined value.

Assertion that expects there to have been zero matching Cased events.

Examples:

Expected no events with a filter inside of a block

def 
  assert_no_cased_events action: 'bot.create' do
    create(:bot)
  end
end

Expected no events inside of a block

def 
  assert_no_cased_events do
    create(:bot)
  end
end

Expected no events containing a subset of the event body for the duration of the test

def 
  create(:bot)

  assert_no_cased_events action: 'bot.create'
end

Expected no events for the duration of the test

def 
  create(:bot)

  assert_no_cased_events
end

Parameters:

  • expected_event_body (Hash) (defaults to: nil)

    Expected event not to be published to Cased.



129
130
131
# File 'lib/cased/test_helper.rb', line 129

def assert_no_cased_events(expected_event_body = nil, &block)
  assert_cased_events(0, expected_event_body, &block)
end

#before_setupObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/cased/test_helper.rb', line 7

def before_setup
  @original_cased_publishers = Cased.publishers
  Cased.publishers = [
    cased_test_publisher,
  ]

  clear_cased_events
  clear_cased_context
  super
end

#cased_eventsObject



33
34
35
# File 'lib/cased/test_helper.rb', line 33

def cased_events
  cased_test_publisher.events
end

#cased_events_with(expected_event = {}) ⇒ Array<Hash>

Locates all published events matching a particular shape.

Examples:

Simple hash

cased_events_with(action: 'user.login') # => [{ action: 'user.login', actor: '[email protected]' }, { action: 'user.login', actor: '[email protected]' }]

Nested hash

cased_events_with(issues: [{ issue_id: 1 }]) # => [{ action: 'user.login', issues: [{ issue_id: 1 }, { issue_id: 2 }]}]

Cased::Model value hash

user = User.new
user.cased_context # => { user: '[email protected]', user_id: 'user_1234' }
cased_events_with(user: user) # => [{ user: '[email protected]', user_id: 'user_1234' }]

Parameters:

  • expected_event (Hash) (defaults to: {})

    the shape of event expected to be published to Cased

Returns:

  • (Array<Hash>)

    Array of matching published Cased events.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cased/test_helper.rb', line 150

def cased_events_with(expected_event = {})
  return cased_events.dup if expected_event.nil?

  if expected_event.empty?
    raise ArgumentError, 'You must call cased_events_with with a non empty Hash otherwise it will match all events'
  end

  expanded_expected_event = Cased::Context::Expander.expand(expected_event)
  if expanded_expected_event.empty?
    raise ArgumentError, <<~MSG.strip
      cased_events_with would have matched any published Cased event.

      cased_events_with was called with #{expected_event.inspect} but resulted into #{expanded_expected_event} after it was expanded.

      This typically happens when an object that includes Cased::Model does not implement either the #cased_id or #to_s method.
    MSG
  end

  # We need to normalize input as it could be a mix of strings and symbols.
  expected_event.deep_symbolize_keys!
  expanded_expected_event = expanded_expected_event.to_a

  events = cased_events.dup.collect(&:deep_symbolize_keys).collect(&:to_a)
  matching_events = events.select do |event|
    diff = expanded_expected_event - event
    diff.empty?
  end

  matching_events.collect(&:to_h)
end

#cased_test_publisherCased::Publishers::TestPublisher

The test published used for the duration of the test.



184
185
186
# File 'lib/cased/test_helper.rb', line 184

def cased_test_publisher
  @cased_test_publisher ||= Cased::Publishers::TestPublisher.new
end

#clear_cased_contextObject



29
30
31
# File 'lib/cased/test_helper.rb', line 29

def clear_cased_context
  Cased::Context.clear!
end

#clear_cased_eventsObject

Clears all published events in the test Cased publisher



25
26
27
# File 'lib/cased/test_helper.rb', line 25

def clear_cased_events
  cased_events.clear
end