如何在minitest中模拟和验证ActionCable传输?

时间:2021-08-17 01:14:20

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

我正在使用带有ActionCable的Ruby on Rails 5.1。我想使用minitest测试一个特定的方法,并模拟“ActionCable.server.broadcast”调用以验证我发送的数据是否正确

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.

但我不知道在我的minitest类中我如何验证ActionCable广播正确的消息数据。

2 个解决方案

#1


5  

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.

Rails ActionCable源已经经过充分测试以确保其正常工作,因此我们知道如果我们只使用正确的参数调用ActionCable就可以进行广播工作。

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.

如果你有一个套接字繁重的应用程序,我建议尝试动作电缆测试,它有很多帮助来验证ActionCable实际上是否广播了什么。

You can check if your method broadcasts X times to a specific channel:

您可以检查您的方法是否向特定频道广播X次:

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).

这个Gem可能很快就会成为Rails核心的一部分,所以它绝对值得一看,特别是如果你正在测试ActionCable的其他部分,这些部分迟早可能需要太多的工作来模拟。 (例如,与特定用户一起登录)。

#2


1  

I suggest using mocks (I use the mocha gem for mocking) to test the broadcast. Here is a simple example:

我建议使用模拟(我使用mocha gem进行模拟)来测试广播。这是一个简单的例子:

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.

这样您就可以模拟所有ActionCable.server调用,但正在测试是否使用正确的参数调用它们。

#1


5  

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.

Rails ActionCable源已经经过充分测试以确保其正常工作,因此我们知道如果我们只使用正确的参数调用ActionCable就可以进行广播工作。

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.

如果你有一个套接字繁重的应用程序,我建议尝试动作电缆测试,它有很多帮助来验证ActionCable实际上是否广播了什么。

You can check if your method broadcasts X times to a specific channel:

您可以检查您的方法是否向特定频道广播X次:

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).

这个Gem可能很快就会成为Rails核心的一部分,所以它绝对值得一看,特别是如果你正在测试ActionCable的其他部分,这些部分迟早可能需要太多的工作来模拟。 (例如,与特定用户一起登录)。

#2


1  

I suggest using mocks (I use the mocha gem for mocking) to test the broadcast. Here is a simple example:

我建议使用模拟(我使用mocha gem进行模拟)来测试广播。这是一个简单的例子:

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.

这样您就可以模拟所有ActionCable.server调用,但正在测试是否使用正确的参数调用它们。