Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mock and verify an ActionCable transmission in minitest?

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.

like image 603
Dave Avatar asked Apr 20 '18 15:04

Dave


1 Answers

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.

like image 70
Laith Azer Avatar answered Nov 17 '22 12:11

Laith Azer