-
I haven't seen any clean way to pass an event into an async event handler. When you create an even and pass it to the
But that is pretty ugly to unit test a handler, any suggestions out there? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @tgsoverly! Thanks for bringing this up. There aren't many ways to do it better right now because of how event = Event.new(data: { test: "data" })
event.metadata[:timestamp] = Time.zone.now
event.metadata[:valid_at] = Time.zone.now
perform_enqueued_jobs(only: Job) do
Rails.configuration.event_store.publish(event)
end Or make this a method and hide the implementation details: class FancyTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
def test_whatever
event = Event.new(data: { test: "data" })
event.metadata[:timestamp] = Time.zone.now
event.metadata[:valid_at] = Time.zone.now
handle_event_async(event, Job)
assert sideffects_of_choice
end
private
def handle_event_async(event, async_handler_class)
perform_enqueued_jobs(only: async_handler_class) do
Rails.configuration.event_store.publish(event)
end
end At least you let |
Beta Was this translation helpful? Give feedback.
-
Long story short:
That said, one pattern that I've practiced in the past was:
|
Beta Was this translation helpful? Give feedback.
-
Sidenote: I made some attempts in the past to redesign async handlers and written my thoughts here — https://gist.github.com/pawelpacana/295337e92dd9edd2e443e6b31ac2683a |
Beta Was this translation helpful? Give feedback.
Hi @tgsoverly!
Thanks for bringing this up. There aren't many ways to do it better right now because of how
ActiveJob::Base
is implemented. However, for the tests relying onActiveJob::TestHelper
we recently found a way to do this more or less like that:Or make this a method and hide the implementation details: