I'm using Ruby on Rails 5.1 with ActionCable. I would like to use minitest to test a particular method, and mock the "ActionCable.server.broadcast" call to verify I'm sending out the right data I have
module MyModule
class Scanner
def trasmit
...
ActionCable.server.broadcast "my_channel", data: msg_data
but I don't know how in my minitest class I can verify that the ActionCable broadcast the correct message data.
I suggest using mocks (I use the mocha gem for mocking) to test the broadcast. Here is a simple example:
channel_name = 'my_channel'
msg_data = 'hello'
broadcast = mock
server = mock
server.expects(:broadcast).with(channel_name, data: msg_data).returns(broadcast)
ActionCable.expects(:server).returns(server)
This way you are mocking all ActionCable.server
calls but are testing that they are called with the right parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With