-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
action-cable-testing migration, pt.1
- Loading branch information
Showing
11 changed files
with
643 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
@rails_post_6 | ||
Feature: have_broadcasted matcher | ||
|
||
The `have_broadcasted_to` (also aliased as `broadcast_to`) matcher is used to check if a message has been broadcasted to a given stream. | ||
|
||
Background: | ||
Given action cable testing is available | ||
|
||
Scenario: Checking stream name | ||
Given a file named "spec/models/broadcaster_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
RSpec.describe "broadcasting" do | ||
it "matches with stream name" do | ||
expect { | ||
ActionCable.server.broadcast( | ||
"notifications", text: 'Hello!' | ||
) | ||
}.to have_broadcasted_to("notifications") | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/broadcaster_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking passed message to stream | ||
Given a file named "spec/models/broadcaster_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
RSpec.describe "broadcasting" do | ||
it "matches with message" do | ||
expect { | ||
ActionCable.server.broadcast( | ||
"notifications", text: 'Hello!' | ||
) | ||
}.to have_broadcasted_to("notifications").with(text: 'Hello!') | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/broadcaster_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking that message passed to stream matches | ||
Given a file named "spec/models/broadcaster_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
RSpec.describe "broadcasting" do | ||
it "matches with message" do | ||
expect { | ||
ActionCable.server.broadcast( | ||
"notifications", text: 'Hello!', user_id: 12 | ||
) | ||
}.to have_broadcasted_to("notifications").with(a_hash_including(text: 'Hello!')) | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/broadcaster_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking passed message with block | ||
Given a file named "spec/models/broadcaster_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
RSpec.describe "broadcasting" do | ||
it "matches with message" do | ||
expect { | ||
ActionCable.server.broadcast( | ||
"notifications", text: 'Hello!', user_id: 12 | ||
) | ||
}.to have_broadcasted_to("notifications").with { |data| | ||
expect(data['user_id']).to eq 12 | ||
} | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/broadcaster_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Using alias method | ||
Given a file named "spec/models/broadcaster_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
RSpec.describe "broadcasting" do | ||
it "matches with stream name" do | ||
expect { | ||
ActionCable.server.broadcast( | ||
"notifications", text: 'Hello!' | ||
) | ||
}.to broadcast_to("notifications") | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/broadcaster_spec.rb` | ||
Then the examples should all pass | ||
|
||
Scenario: Checking broadcast to a record | ||
Given a file named "spec/channels/chat_channel_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
RSpec.describe ChatChannel, :type => :channel do | ||
it "successfully subscribes" do | ||
user = User.new(42) | ||
expect { | ||
ChatChannel.broadcast_to(user, text: 'Hi') | ||
}.to have_broadcasted_to(user) | ||
end | ||
end | ||
""" | ||
And a file named "app/models/user.rb" with: | ||
"""ruby | ||
class User < Struct.new(:name) | ||
def to_gid_param | ||
name | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/channels/chat_channel_spec.rb` | ||
Then the example should pass | ||
|
||
Scenario: Checking broadcast to a record in non-channel spec | ||
Given a file named "spec/models/broadcaster_spec.rb" with: | ||
"""ruby | ||
require "rails_helper" | ||
RSpec.describe "broadcasting" do | ||
it "matches with stream name" do | ||
user = User.new(42) | ||
expect { | ||
ChatChannel.broadcast_to(user, text: 'Hi') | ||
}.to broadcast_to(ChatChannel.broadcasting_for(user)) | ||
end | ||
end | ||
""" | ||
And a file named "app/models/user.rb" with: | ||
"""ruby | ||
class User < Struct.new(:name) | ||
def to_gid_param | ||
name | ||
end | ||
end | ||
""" | ||
When I run `rspec spec/models/broadcaster_spec.rb` | ||
Then the example should pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "rspec/rails/matchers/action_cable/have_broadcasted_to" | ||
|
||
module RSpec | ||
module Rails | ||
module Matchers | ||
# Namespace for various implementations of ActionCable features | ||
# | ||
# @api private | ||
module ActionCable | ||
end | ||
|
||
# @api public | ||
# Passes if a message has been sent to a stream/object inside a block. | ||
# May chain `at_least`, `at_most` or `exactly` to specify a number of times. | ||
# To specify channel from which message has been broadcasted to object use `from_channel`. | ||
# | ||
# | ||
# @example | ||
# expect { | ||
# ActionCable.server.broadcast "messages", text: 'Hi!' | ||
# }.to have_broadcasted_to("messages") | ||
# | ||
# expect { | ||
# SomeChannel.broadcast_to(user) | ||
# }.to have_broadcasted_to(user).from_channel(SomeChannel) | ||
# | ||
# # Using alias | ||
# expect { | ||
# ActionCable.server.broadcast "messages", text: 'Hi!' | ||
# }.to broadcast_to("messages") | ||
# | ||
# expect { | ||
# ActionCable.server.broadcast "messages", text: 'Hi!' | ||
# ActionCable.server.broadcast "all", text: 'Hi!' | ||
# }.to have_broadcasted_to("messages").exactly(:once) | ||
# | ||
# expect { | ||
# 3.times { ActionCable.server.broadcast "messages", text: 'Hi!' } | ||
# }.to have_broadcasted_to("messages").at_least(2).times | ||
# | ||
# expect { | ||
# ActionCable.server.broadcast "messages", text: 'Hi!' | ||
# }.to have_broadcasted_to("messages").at_most(:twice) | ||
# | ||
# expect { | ||
# ActionCable.server.broadcast "messages", text: 'Hi!' | ||
# }.to have_broadcasted_to("messages").with(text: 'Hi!') | ||
def have_broadcasted_to(target = nil) | ||
check_action_cable_adapter | ||
|
||
ActionCable::HaveBroadcastedTo.new(target, :channel => described_class) | ||
end | ||
alias_method :broadcast_to, :have_broadcasted_to | ||
|
||
private | ||
|
||
# @private | ||
def check_action_cable_adapter | ||
return if ::ActionCable::SubscriptionAdapter::Test === ::ActionCable.server.pubsub | ||
|
||
raise StandardError, "To use ActionCable matchers set `adapter: test` in your cable.yml" | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.