I'm writing a request test in RSpec and in the course of the controller path I am testing, the application sends out two turbo stream broadcasts.
I have not been able to get the broadcast_to
expectation to work in this case. I feel I am close, but I have hit a wall and thought I'd ask a question while continuing to do research.
Context
The models in question are a press release, which has many press release image models. Each press release image model has one attached image.
The controller action takes a file attachment and attaches it to the press release model, then broadcasts an update targeting a frame:
image_model.broadcast_replace_to(image_model.press_release, target: "slot_#{image_model.position}"
The reason we do image_model.press_release
to pass in *streamable
is because our view starts a Turbo stream via turbo_stream_from(@press_release)
so we need to target the right stream.
Problem
I have this expectation and it is just not hitting right:
expect do
post press_release_images_path(press_release), params: params,
headers: { 'Content-Type' => 'multipart/form-data' }
end.to broadcast_to(press_release).from_channel(Turbo::StreamsChannel)
This snippet seems to at least let the test run, but it fails as there are 0 broadcasts to this channel. What's curious though, is that in my test logs when running this test, I see a similiar stream name.
Failure messsage from above expect block:
expected to broadcast exactly 1 messages to turbo:streams:Z2lkOi8vcHJlc3MtcmVsZWFzZS1nZW5lcmF0b3IvUHJlc3NSZWxlYXNlLzE5NDM, but broadcast 0
Logs of test controller action:
Rendered press_releases/images/_display.html.erb (Duration: 15.2ms | Allocations: 9700) [ActionCable] Broadcasting to Z2lkOi8vcHJlc3MtcmVsZWFzZS1nZW5lcmF0b3IvUHJlc3NSZWxlYXNlLzE5NDM: "<turbo-stream action=\"update\"...
There is a second broadcast as well, but the same issue persists: I seemingly have the overall direction correct, but can't seem to figure out how to match the stream name. The generated strings are present in both the RSpec error message and the testlogs, but I am not sure how to reconcile them to get the test passing.
While I would be keen to solve the problem along the same shape that I've outlined here, I am open to alternative approaches to testing this behaviour!