Module: DurableStreams::Rails::Broadcastable::TestHelper

Extended by:
ActiveSupport::Concern
Defined in:
lib/durable_streams/rails/broadcastable/test_helper.rb

Instance Method Summary collapse

Instance Method Details

#assert_no_stream_broadcasts(stream_name_or_object, &block) ⇒ Object

Asserts that no State Protocol events were broadcast to a stream

Arguments

  • stream_name_or_object the objects used to generate the stream name, or the name itself

  • &block optional block executed before the assertion

Asserts that no events were broadcast:

assert_no_stream_broadcasts "messages" do
  # do something other than broadcast to "messages"
end

In addition to a String, the helper also accepts an Object or Array to determine the stream name:

post = Post.find(1)

assert_no_stream_broadcasts post do
  # do something other than broadcast to post's stream
end


96
97
98
99
100
101
102
103
# File 'lib/durable_streams/rails/broadcastable/test_helper.rb', line 96

def assert_no_stream_broadcasts(stream_name_or_object, &block)
  block&.call

  stream_name = stream_name_from(stream_name_or_object)
  payloads = stream_broadcasts_for(stream_name)

  assert payloads.empty?, "Expected no broadcasts on #{stream_name.inspect}, but there were #{payloads.count}"
end

#assert_stream_broadcasts(stream_name_or_object, count: nil, &block) ⇒ Object

Asserts that State Protocol events were broadcast to a stream

Arguments

  • stream_name_or_object the objects used to generate the stream name, or the name itself

  • &block optional block executed before the assertion

Options

  • count: the number of events that are expected to be broadcast

Asserts events were broadcast:

comment = Comment.find(1)
comment.stream_insert_to comment.post

assert_stream_broadcasts comment.post

Asserts that two events were broadcast:

comment = Comment.find(1)
comment.stream_insert_to comment.post
comment.stream_update_to comment.post

assert_stream_broadcasts comment.post, count: 2

You can pass a block to run before the assertion:

comment = Comment.find(1)

assert_stream_broadcasts comment.post do
  comment.stream_insert_to comment.post
end

In addition to a String, the helper also accepts an Object or Array to determine the stream name:

post = Post.find(1)

assert_stream_broadcasts post do
  post.comments.create!(body: "Hello")
end


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/durable_streams/rails/broadcastable/test_helper.rb', line 59

def assert_stream_broadcasts(stream_name_or_object, count: nil, &block)
  payloads = capture_stream_broadcasts(stream_name_or_object, &block)
  stream_name = stream_name_from(stream_name_or_object)

  if count.nil?
    assert_not_empty payloads, "Expected at least one broadcast on #{stream_name.inspect}, but there were none"
  else
    broadcasts = "Durable Stream broadcast".pluralize(count)

    assert count == payloads.count, "Expected #{count} #{broadcasts} on #{stream_name.inspect}, but there were #{payloads.count}"
  end
end

#capture_stream_broadcasts(stream_name_or_object, &block) ⇒ Object

Captures any State Protocol events that were broadcast to a stream

Arguments

  • stream_name_or_object the objects used to generate the stream name, or the name itself

  • &block optional block to capture broadcasts during execution

Returns any events that have been broadcast as an Array of parsed JSON hashes

comment = Comment.find(1)
comment.stream_insert_to comment.post
comment.stream_update_to comment.post

events = capture_stream_broadcasts comment.post

assert_equal "insert", events.first["headers"]["operation"]
assert_equal "update", events.second["headers"]["operation"]

You can pass a block to limit the scope of the broadcasts being captured:

comment = Comment.find(1)

events = capture_stream_broadcasts comment.post do
  comment.stream_insert_to comment.post
end

assert_equal "insert", events.first["headers"]["operation"]


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/durable_streams/rails/broadcastable/test_helper.rb', line 134

def capture_stream_broadcasts(stream_name_or_object, &block)
  stream_name = stream_name_from(stream_name_or_object)

  if block_given?
    before = stream_broadcasts_for(stream_name).size
    block.call
    stream_broadcasts_for(stream_name)[before..]
  else
    stream_broadcasts_for(stream_name)
  end
end