Skip to content

Commit

Permalink
feat: make it Rails 6 compatible and conflict-free
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Feb 25, 2019
1 parent b4a7b38 commit 1048abb
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ matrix:
gemfile: Gemfile
- rvm: 2.6
gemfile: Gemfile
- rvm: 2.6
gemfile: gemfiles/rails6.gemfile
- rvm: 2.5
gemfile: Gemfile
- rvm: 2.3
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

This gem provides missing testing utils for [Action Cable][].

**NOTE:** this gem [has](https://github.com/rails/rails/pull/33659) [been](https://github.com/rails/rails/pull/33969) [merged](https://github.com/rails/rails/pull/34845) into Rails 6.0 and no longer needed.
**NOTE:** this gem [has](https://github.com/rails/rails/pull/33659) [been](https://github.com/rails/rails/pull/33969) [merged](https://github.com/rails/rails/pull/34845) into Rails 6.0.

If you're using Minitest – you don't need this gem anymore.

If you're using RSpec < 4, you still can use this gem to write Action Cable specs even for Rails 6.


## Installation
Expand Down Expand Up @@ -93,6 +97,7 @@ end

\* **NOTE:** in Rails 6.0 you should use `.broadcasting_for`, but it's not backward compatible
and we cannot use it in Rails 5.x. See https://github.com/rails/rails/pull/35021.
Note also, that this feature hasn't been released in Rails 6.0.0.beta1, so you still need the refinement.

### Channels Testing

Expand Down
2 changes: 1 addition & 1 deletion action-cable-testing.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 2.3.0"

spec.add_dependency "actioncable", "~> 5.0"
spec.add_dependency "actioncable", ">= 5.0"

spec.add_development_dependency "bundler", ">= 1.10"
spec.add_development_dependency "cucumber", "~> 3.1.1"
Expand Down
5 changes: 5 additions & 0 deletions gemfiles/rails6.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem "rails", "6.0.0.beta1"

gemspec path: '..'
26 changes: 10 additions & 16 deletions lib/action_cable/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@
require "action_cable"
require "action_cable/testing/rails_six"

module ActionCable
autoload :TestCase
autoload :TestHelper
# These has been merged into Rails 6
unless ActionCable::VERSION::MAJOR >= 6
require "action_cable/testing/test_helper"
require "action_cable/testing/test_case"

module Channel
eager_autoload do
autoload :TestCase
end
end
require "action_cable/testing/channel/test_case"

module Connection
eager_autoload do
autoload :TestCase
end
end
require "action_cable/testing/connection/test_case"

module SubscriptionAdapter
autoload :Test
end
# We cannot move subsription adapter under 'testing/' path,
# 'cause Action Cable uses this path when resolving an
# adapter from its name (in the config.yml)
require "action_cable/subscription_adapter/test"
end
File renamed without changes.
File renamed without changes.
9 changes: 6 additions & 3 deletions lib/action_cable/testing/rails_six.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ module Testing
# using ActionCable::Testing::Syntax
module Rails6
begin
refine ActionCable::Channel::Broadcasting::ClassMethods do
def broadcasting_for(model)
super([channel_name, model])
# Has been added only after 6.0.0.beta1
unless ActionCable::Channel.respond_to?(:serialize_broadcasting)
refine ActionCable::Channel::Broadcasting::ClassMethods do
def broadcasting_for(model)
super([channel_name, model])
end
end
end

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def stream(target, channel = nil)
return target if target.is_a?(String)

channel ||= @subscription
raise CHANNEL_NOT_FOUND unless channel && channel.respond_to?(:channel_name)
return target unless channel && channel.respond_to?(:channel_name)

channel.broadcasting_for([channel.channel_name, target])
end
Expand Down
6 changes: 4 additions & 2 deletions spec/support/deprecated_api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
DeprecatedApi = Module.new do
def self.enabled?
Gem::Version.new(ActionCable::Testing::VERSION) < Gem::Version.new("1.0") ||
raise("Deprecated API should be removed")
ActionCable::VERSION::MAJOR < 6 && (
Gem::Version.new(ActionCable::Testing::VERSION) < Gem::Version.new("1.0") ||
raise("Deprecated API should be removed")
)
end
end
4 changes: 0 additions & 4 deletions test/connection/test_case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ def test_deprecated_cookie

assert_equal "456", connection.user_id
end
else
def test_deprecated_cookie
assert_reject_connection { connect cookies: { user_id: "456" } }
end
end

def test_disconnect
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_deprecated_assert_broadcast_to_object_with_channel
def test_assert_broadcast_to_object_without_channel
user = User.new(42)

assert_raises ArgumentError do
assert_raises Minitest::Assertion do
assert_broadcasts user, 1 do
BroadcastChannel.broadcast_to user, text: "text"
end
Expand Down Expand Up @@ -172,7 +172,7 @@ def test_deprecated_assert_broadcast_om_object_with_channel
def test_assert_broadcast_on_object_without_channel
user = User.new(42)

assert_raises ArgumentError do
assert_raises Minitest::Assertion do
assert_broadcast_on user, text: "text" do
BroadcastChannel.broadcast_to user, text: "text"
end
Expand Down

0 comments on commit 1048abb

Please sign in to comment.