Introduce `capture_emails` and `capture_broadcasts` (#48798)

This commit is contained in:
Alex Ghiculescu 2023-07-25 14:42:54 +10:00 committed by GitHub
parent 5f3eb2195b
commit 455e922b49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 55 deletions

View File

@ -13,17 +13,12 @@
*Joé Dupuis*
* `assert_broadcasts` now returns the messages that were broadcast.
* Introduce the `capture_broadcasts` test helper.
This makes it easier to do further analysis on those messages:
Returns all messages broadcast in a block.
```ruby
message = assert_broadcasts("test", 1) do
ActionCable.server.broadcast "test", "message"
end
assert_equal "message", message
messages = assert_broadcasts("test", 2) do
messages = capture_broadcasts("test") do
ActionCable.server.broadcast "test", { message: "one" }
ActionCable.server.broadcast "test", { message: "two" }
end

View File

@ -52,12 +52,6 @@ module ActionCable
actual_count = new_messages.size
assert_equal number, actual_count, "#{number} broadcasts to #{stream} expected, but #{actual_count} were sent"
if new_messages.size == 1
ActiveSupport::JSON.decode(new_messages.first)
else
new_messages.map { |m| ActiveSupport::JSON.decode(m) }
end
else
actual_count = broadcasts(stream).size
assert_equal number, actual_count, "#{number} broadcasts to #{stream} expected, but #{actual_count} were sent"
@ -88,6 +82,22 @@ module ActionCable
assert_broadcasts stream, 0, &block
end
# Returns the messages that are broadcasted in the block.
#
# def test_broadcasts
# messages = capture_broadcasts('messages') do
# ActionCable.server.broadcast 'messages', { text: 'hi' }
# ActionCable.server.broadcast 'messages', { text: 'how are you?' }
# end
# assert_equal 2, messages.length
# assert_equal({ text: 'hi' }, messages.first)
# assert_equal({ text: 'how are you?' }, messages.last)
# end
#
def capture_broadcasts(stream, &block)
new_broadcasts_from(broadcasts(stream), stream, "capture_broadcasts", &block).map { |m| ActiveSupport::JSON.decode(m) }
end
# Asserts that the specified message has been sent to the stream.
#
# def test_assert_transmitted_message

View File

@ -14,13 +14,13 @@ class TransmissionsTest < ActionCable::TestCase
end
end
def test_assert_broadcasts_returns_broadcast_messages_if_block_given
message = assert_broadcasts("test", 1) do
def test_capture_broadcasts
messages = capture_broadcasts("test") do
ActionCable.server.broadcast "test", "message"
end
assert_equal "message", message
assert_equal "message", messages.first
messages = assert_broadcasts("test", 2) do
messages = capture_broadcasts("test") do
ActionCable.server.broadcast "test", { message: "one" }
ActionCable.server.broadcast "test", { message: "two" }
end

View File

@ -86,18 +86,13 @@
*Sean Doyle*
* `assert_emails` now returns the emails that were sent.
* Introduce the `capture_emails` test helper.
This makes it easier to do further analysis on those emails:
Returns all emails that are sent in a block.
```ruby
def test_emails_more_thoroughly
email = assert_emails 1 do
ContactMailer.welcome.deliver_now
end
assert_email "Hi there", email.subject
emails = assert_emails 2 do
def test_emails
emails = capture_emails do
ContactMailer.welcome.deliver_now
ContactMailer.welcome.deliver_later
end

View File

@ -32,34 +32,10 @@ module ActionMailer
# ContactMailer.welcome.deliver_later
# end
# end
#
# The method returns the +Mail::Message+s that were processed, enabling further
# analysis.
#
# def test_emails_more_thoroughly
# email = assert_emails 1 do
# ContactMailer.welcome.deliver_now
# end
# assert_equal "Hi there", email.subject
#
# emails = assert_emails 2 do
# ContactMailer.welcome.deliver_now
# ContactMailer.welcome.deliver_later
# end
# assert_equal "Hi there", emails.first.subject
# end
def assert_emails(number, &block)
if block_given?
original_count = ActionMailer::Base.deliveries.size
deliver_enqueued_emails(&block)
new_count = ActionMailer::Base.deliveries.size
diff = new_count - original_count
diff = capture_emails(&block).length
assert_equal number, diff, "#{number} emails expected, but #{diff} were sent"
if diff == 1
ActionMailer::Base.deliveries.last
else
ActionMailer::Base.deliveries.last(diff)
end
else
assert_equal number, ActionMailer::Base.deliveries.size
end
@ -276,6 +252,28 @@ module ActionMailer
perform_enqueued_jobs(only: ->(job) { delivery_job_filter(job) }, queue: queue, at: at, &block)
end
# Returns any emails that are sent in the block.
#
# def test_emails
# emails = capture_emails do
# ContactMailer.welcome.deliver_now
# end
# assert_equal "Hi there", emails.first.subject
#
# emails = capture_emails do
# ContactMailer.welcome.deliver_now
# ContactMailer.welcome.deliver_later
# end
# assert_equal "Hi there", emails.first.subject
# end
def capture_emails(&block)
original_count = ActionMailer::Base.deliveries.size
deliver_enqueued_emails(&block)
new_count = ActionMailer::Base.deliveries.size
diff = new_count - original_count
ActionMailer::Base.deliveries.last(diff)
end
private
def delivery_job_filter(job)
job_class = job.is_a?(Hash) ? job.fetch(:job) : job.class

View File

@ -95,16 +95,17 @@ class TestHelperMailerTest < ActionMailer::TestCase
end
end
def test_assert_emails_returns_the_emails_that_were_sent_if_a_block_is_given
def test_capture_emails
assert_nothing_raised do
email = assert_emails 1 do
emails = capture_emails do
TestHelperMailer.test.deliver_now
end
email = emails.first
assert_instance_of Mail::Message, email
assert_equal "Hello, Earth", email.body.to_s
assert_equal "Hi!", email.subject
emails = assert_emails 2 do
emails = capture_emails do
TestHelperMailer.test.deliver_now
TestHelperMailer.test.deliver_now
end