Using rails 7.1.2 and rspec-rails 6.1.0
TL;TR Tests fail only when broadcasting "later" in background jobs by using config.include ActiveJob::TestHelper in the rails_helper.rb file, but they pass when not using it.
# app/models/my_class.rb
class MyClass < ApplicationRecord
after_update_commit :broadcast_update_user
private
def broadcast_update_user
broadcast_update_later_to('ts_channel_id', ...)
end
end
# system/my_class_spec.rb
RSpec.describe 'MyClass system tests', :type => :system do
it 'should broadcast', :js => true do
@my_instance = FactoryBot.create(:regular_instance)
visit my_path
# simulate record update
@my_instance.update!(:value => 'YO!!!')
expect(page).to have_css('span.value-target', :text => 'YO!!!', :exact_text => true, :count => 1)
end
end
If I run the above spec by using the following RSpec configuration, then the test fails.
# rails_helper.rb
RSpec.configure do |config|
config.include ActiveJob::TestHelper
config.before(:suite) do
ActiveJob::Base.queue_adapter = :async
end
end
If I run the above spec by using the following RSpec configuration, then the test passes.
# rails_helper.rb
RSpec.configure do |config|
# config.include ActiveJob::TestHelper
config.before(:suite) do
ActiveJob::Base.queue_adapter = :async
end
end
How can I solve the testing issue related to broadcasting later in a background job?
RSpec logs
By using config.include ActiveJob::TestHelper (TEST FAILS)
...
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
Turbo::StreamsChannel is transmitting the subscription confirmation
Turbo::StreamsChannel is streaming from ...
[ActiveJob] Enqueued Turbo::Streams::ActionBroadcastJob (Job ID: 58b516a5-8187-4b70-a02b-0715a8568961) to Test(default) with arguments: "ts_channel_id", ...
Finished "/cable" [WebSocket] for 127.0.0.1 at 2024-01-18 18:35:29 +0100
Turbo::StreamsChannel stopped streaming from ts_channel_id
...
By not using config.include ActiveJob::TestHelper (TEST PASSES)
...
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
Turbo::StreamsChannel is transmitting the subscription confirmation
Turbo::StreamsChannel is streaming from ts_channel_id
[ActiveJob] Enqueued Turbo::Streams::ActionBroadcastJob (Job ID: 94450a63-1f99-4082-9b54-51991d35db56) to Async(default) with arguments: "ts_channel_id", ...
[ActiveJob] [Turbo::Streams::ActionBroadcastJob] [94450a63-1f99-4082-9b54-51991d35db56] Performing Turbo::Streams::ActionBroadcastJob (Job ID: 94450a63-1f99-4082-9b54-51991d35db56) from Async(default) enqueued at 2024-01-18T17:36:29.217779000Z with arguments: "ts_channel_id", ...
[ActiveJob] [Turbo::Streams::ActionBroadcastJob] [94450a63-1f99-4082-9b54-51991d35db56] [ActionCable] Broadcasting to ts_channel_id: ...
[ActiveJob] [Turbo::Streams::ActionBroadcastJob] [94450a63-1f99-4082-9b54-51991d35db56] Performed Turbo::Streams::ActionBroadcastJob (Job ID: 94450a63-1f99-4082-9b54-51991d35db56) from Async(default) in 15.67ms
Turbo::StreamsChannel transmitting ...
Finished "/cable" [WebSocket] for 127.0.0.1 at 2024-01-18 18:36:29 +0100
Turbo::StreamsChannel stopped streaming from ts_channel_id
...
Notes:
- No problems in development and production: broadcasting later in background jobs just works.
- Testing by using the
broadcast_update_tomethod (instead ofbroadcast_update_later_to) makes the test to pass, but I need to broadcast "later". - Maybe the issue is someway related to this one but I'm unsure.