提供用于测试 Action Cable 广播的辅助方法
方法
实例公有方法
assert_broadcast_on(stream, data, &block) 链接
断言指定的消息已发送到流。
def test_assert_transmitted_message
ActionCable.server.broadcast 'messages', text: 'hello'
assert_broadcast_on('messages', text: 'hello')
end
如果传递了一个块,则该块应导致发送具有指定数据的消息。
def test_assert_broadcast_on_again
assert_broadcast_on('messages', text: 'hello') do
ActionCable.server.broadcast 'messages', text: 'hello'
end
end
源代码:显示 | 在 GitHub 上
# File actioncable/lib/action_cable/test_helper.rb, line 116 def assert_broadcast_on(stream, data, &block) # Encode to JSON and back–we want to use this value to compare with decoded # JSON. Comparing JSON strings doesn't work due to the order if the keys. serialized_msg = ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(data)) new_messages = broadcasts(stream) if block_given? new_messages = new_broadcasts_from(new_messages, stream, "assert_broadcast_on", &block) end message = new_messages.find { |msg| ActiveSupport::JSON.decode(msg) == serialized_msg } error_message = "No messages sent with #{data} to #{stream}" if new_messages.any? error_message = new_messages.inject("#{error_message}\nMessage(s) found:\n") do |error_message, new_message| error_message + "#{ActiveSupport::JSON.decode(new_message)}\n" end else error_message = "#{error_message}\nNo message found for #{stream}" end assert message, error_message end
assert_broadcasts(stream, number, &block) 链接
断言广播到流的消息数量与给定数量匹配。
def test_broadcasts
assert_broadcasts 'messages', 0
ActionCable.server.broadcast 'messages', { text: 'hello' }
assert_broadcasts 'messages', 1
ActionCable.server.broadcast 'messages', { text: 'world' }
assert_broadcasts 'messages', 2
end
如果传递了一个块,则该块应导致指定数量的消息被广播。
def test_broadcasts_again
assert_broadcasts('messages', 1) do
ActionCable.server.broadcast 'messages', { text: 'hello' }
end
assert_broadcasts('messages', 2) do
ActionCable.server.broadcast 'messages', { text: 'hi' }
ActionCable.server.broadcast 'messages', { text: 'how are you?' }
end
end
源代码:显示 | 在 GitHub 上
# File actioncable/lib/action_cable/test_helper.rb, line 48 def assert_broadcasts(stream, number, &block) if block_given? new_messages = new_broadcasts_from(broadcasts(stream), stream, "assert_broadcasts", &block) actual_count = new_messages.size assert_equal number, actual_count, "#{number} broadcasts to #{stream} expected, but #{actual_count} were sent" else actual_count = broadcasts(stream).size assert_equal number, actual_count, "#{number} broadcasts to #{stream} expected, but #{actual_count} were sent" end end
assert_no_broadcasts(stream, &block) 链接
断言没有消息发送到流。
def test_no_broadcasts
assert_no_broadcasts 'messages'
ActionCable.server.broadcast 'messages', { text: 'hi' }
assert_broadcasts 'messages', 1
end
如果传递了一个块,则该块不应导致任何消息被发送。
def test_broadcasts_again
assert_no_broadcasts 'messages' do
# No job messages should be sent from this block
end
end
注意:此断言只是
assert_broadcasts 'messages', 0, &block
源代码:显示 | 在 GitHub 上
# File actioncable/lib/action_cable/test_helper.rb, line 80 def assert_no_broadcasts(stream, &block) assert_broadcasts stream, 0, &block end
capture_broadcasts(stream, &block) 链接
返回在块中广播的消息。
def test_broadcasts
messages = capture_broadcasts('messages') do
ActionCable.server.broadcast 'messages', { text: 'hi' }
ActionCable.server.broadcast 'messages', { text: 'how are you?' }
end
assert_equal 2, messages.length
assert_equal({ text: 'hi' }, messages.first)
assert_equal({ text: 'how are you?' }, messages.last)
end
源代码:显示 | 在 GitHub 上
# File actioncable/lib/action_cable/test_helper.rb, line 96 def capture_broadcasts(stream, &block) new_broadcasts_from(broadcasts(stream), stream, "capture_broadcasts", &block).map { |m| ActiveSupport::JSON.decode(m) } end