Skip to content

Instantly share code, notes, and snippets.

@SunDi3yansyah
Forked from tomekw/test_channel_spec.rb
Created May 31, 2020 10:36
Show Gist options
  • Save SunDi3yansyah/f352625162251f671a2d7bafbcc8455c to your computer and use it in GitHub Desktop.
Save SunDi3yansyah/f352625162251f671a2d7bafbcc8455c to your computer and use it in GitHub Desktop.

Revisions

  1. @tomekw tomekw revised this gist Jun 5, 2017. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions test_channel_spec.rb
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,16 @@
    # app/channels/test_channel.rb
    class TestChannel < ActionCable::Channel::Base
    def test_action(data)
    times_to_broadcast = data.fetch("times_to_broadcast")

    times_to_broadcast.times do
    ActionCable.server.broadcast("test", current_profile.name)
    # app/channels/hello_channel.rb
    class HelloChannel < ActionCable::Channel::Base
    def say_hello(data)
    times_to_say_hello = data.fetch("times_to_say_hello")
    hello = "Hello, #{current_profile.name}!"

    times_to_say_hello.times do
    ActionCable.server.broadcast(current_profile.id, hello)
    end
    end
    end

    # spec/channels_test_channel_spec.rb
    # spec/channels/hello_channel_spec.rb
    require "spec_helper"

    # This is the minimal ActionCable connection stub to make the test pass
    @@ -29,10 +30,10 @@ def initialize(identifiers_hash = {})
    end
    end

    RSpec.describe TestChannel do
    RSpec.describe HelloChannel do
    subject(:channel) { described_class.new(connection, {}) }

    let(:current_profile) { double(name: "Bob") }
    let(:current_profile) { double(id: "1", name: "Bob") }

    # Connection is `identified_by :current_profile`
    let(:connection) { TestConnection.new(current_profile: current_profile) }
    @@ -44,12 +45,12 @@ def initialize(identifiers_hash = {})
    let(:data) do
    {
    "action" => "test_action",
    "times_to_broadcast" => 3
    "times_to_say_hello" => 3
    }
    end

    it "brodcasts 'Bob' 3 times" do
    expect(action_cable).to receive(:broadcast).with("test", "Bob").exactly(3).times
    it "broadcasts 'Hello, Bob!' 3 times" do
    expect(action_cable).to receive(:broadcast).with("1", "Hello, Bob!").exactly(3).times

    channel.perform_action(data)
    end
  2. @tomekw tomekw created this gist Jun 4, 2017.
    56 changes: 56 additions & 0 deletions test_channel_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    # app/channels/test_channel.rb
    class TestChannel < ActionCable::Channel::Base
    def test_action(data)
    times_to_broadcast = data.fetch("times_to_broadcast")

    times_to_broadcast.times do
    ActionCable.server.broadcast("test", current_profile.name)
    end
    end
    end

    # spec/channels_test_channel_spec.rb
    require "spec_helper"

    # This is the minimal ActionCable connection stub to make the test pass
    class TestConnection
    attr_reader :identifiers, :logger

    def initialize(identifiers_hash = {})
    @identifiers = identifiers_hash.keys
    @logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(StringIO.new))

    # This is an equivalent of providing `identified_by :identifier_key` in ActionCable::Connection::Base subclass
    identifiers_hash.each do |identifier, value|
    define_singleton_method(identifier) do
    value
    end
    end
    end
    end

    RSpec.describe TestChannel do
    subject(:channel) { described_class.new(connection, {}) }

    let(:current_profile) { double(name: "Bob") }

    # Connection is `identified_by :current_profile`
    let(:connection) { TestConnection.new(current_profile: current_profile) }

    let(:action_cable) { ActionCable.server }

    # ActionCable dispatches actions by the `action` attribute.
    # In this test we assume the payload was successfully parsed (it could be a JSON payload, for example).
    let(:data) do
    {
    "action" => "test_action",
    "times_to_broadcast" => 3
    }
    end

    it "brodcasts 'Bob' 3 times" do
    expect(action_cable).to receive(:broadcast).with("test", "Bob").exactly(3).times

    channel.perform_action(data)
    end
    end