Skip to content

Commit

Permalink
Merge pull request rails#47204 from StephaneRob/feat-improve-assert-b…
Browse files Browse the repository at this point in the history
…roadcast-on-message

feat: improve `assert_broadcast_on` error message
  • Loading branch information
byroot authored Feb 4, 2023
2 parents 4b560ab + 1333260 commit 3d0d027
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions actioncable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Display broadcasted messages on error message when using `assert_broadcast_on`

*Stéphane Robino*

* The Action Cable client now supports subprotocols to allow passing arbitrary data
to the server.

Expand Down
12 changes: 11 additions & 1 deletion actioncable/lib/action_cable/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ def assert_broadcast_on(stream, data, &block)

message = new_messages.find { |msg| ActiveSupport::JSON.decode(msg) == serialized_msg }

assert message, "No messages sent with #{data} to #{stream}"
error_message = "No messages sent with #{data} to #{stream}"

if new_messages.any?
error_message = new_messages.inject("#{error_message}\nMessage(s) found:\n") do |error_message, new_message|
error_message + "#{ActiveSupport::JSON.decode(new_message)}\n"
end
else
error_message = "#{error_message}\nNo message found for #{stream}"
end

assert message, error_message
end

def pubsub_adapter # :nodoc:
Expand Down
10 changes: 10 additions & 0 deletions actioncable/test/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,15 @@ def test_assert_broadcast_on_message
end

assert_match(/No messages sent/, error.message)
assert_match(/Message\(s\) found:\nhello/, error.message)
end

def test_assert_broadcast_on_message_with_empty_channel
error = assert_raises Minitest::Assertion do
assert_broadcast_on("test", "world")
end

assert_match(/No messages sent/, error.message)
assert_match(/No message found for test/, error.message)
end
end

0 comments on commit 3d0d027

Please sign in to comment.