Skip to content

Best way to test async listeners of an event #1206

Answered by fidel
tgsoverly asked this question in Q&A
Discussion options

You must be logged in to vote

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 on ActiveJob::TestHelper we recently found a way to do this more or less like that:

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[:times…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by fidel
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants