Rails ActionCable source is already well enough tested to ensure it works, so we know that broadcasting work if we just call ActionCable with the right parameters.
If you have a socket-heavy application I recommend trying out action-cable-testing which has lots of helper to verify that ActionCable actually broadcasts something.
You can check if your method broadcasts X times to a specific channel:
class ScannerTest < ActionDispatch::IntegrationTest
include ActionCable::TestHelper
def test_my_broadcaster
channel_name = 'my_channel'
assert_broadcasts channel_name, 0
# run your method (e.g. trasmit)
assert_broadcasts channel_name, 1
end
end
Or verify that the expected data was sent to the channel:
class ScannerTest < ActionDispatch::IntegrationTest
include ActionCable::TestHelper
def test_my_broadcaster
channel_name = 'my_channel'
expected_data = { :hello => "world" }
assert_broadcast_on(channel_name, data: expected_data) do
# run your trasmit method here which should call:
ActionCable.server.broadcast channel_name, data: expected_data
end
end
end
This Gem might be part of the Rails core soon so it's definitely worth a look especially if you are testing other parts of ActionCable which sooner or later might be too much work to mock. (E.g. sign in with a specific user).