Skip to content

Commit 66b4379

Browse files
committed
Migrate applicable actioncable tests to use NotificationAssertions
1 parent 6a62eed commit 66b4379

File tree

2 files changed

+48
-80
lines changed

2 files changed

+48
-80
lines changed

actioncable/test/channel/base_test.rb

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -199,67 +199,49 @@ def error_handler
199199
end
200200

201201
test "notification for perform_action" do
202-
events = []
203-
ActiveSupport::Notifications.subscribe("perform_action.action_cable") { |event| events << event }
204-
205202
data = { "action" => :speak, "content" => "hello" }
206-
@channel.perform_action data
203+
expected_payload = { channel_class: "ActionCable::Channel::BaseTest::ChatChannel", action: :speak, data: }
207204

208-
assert_equal 1, events.length
209-
assert_equal "perform_action.action_cable", events[0].name
210-
assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
211-
assert_equal :speak, events[0].payload[:action]
212-
assert_equal data, events[0].payload[:data]
213-
ensure
214-
ActiveSupport::Notifications.unsubscribe "perform_action.action_cable"
205+
assert_notifications_count("perform_action.action_cable", 1) do
206+
assert_notification("perform_action.action_cable", expected_payload) do
207+
@channel.perform_action data
208+
end
209+
end
215210
end
216211

217212
test "notification for transmit" do
218-
events = []
219-
ActiveSupport::Notifications.subscribe("transmit.action_cable") { |event| events << event }
213+
data = { data: "latest" }
214+
expected_payload = { channel_class: "ActionCable::Channel::BaseTest::ChatChannel", data:, via: nil }
220215

221-
@channel.perform_action "action" => :get_latest
222-
expected_data = { data: "latest" }
223-
224-
assert_equal 1, events.length
225-
assert_equal "transmit.action_cable", events[0].name
226-
assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
227-
assert_equal expected_data, events[0].payload[:data]
228-
assert_nil events[0].payload[:via]
229-
ensure
230-
ActiveSupport::Notifications.unsubscribe "transmit.action_cable"
216+
assert_notifications_count("transmit.action_cable", 1) do
217+
assert_notification("transmit.action_cable", expected_payload) do
218+
@channel.perform_action "action" => :get_latest
219+
end
220+
end
231221
end
232222

233223
test "notification for transmit_subscription_confirmation" do
234-
@channel.subscribe_to_channel
235-
236-
events = []
237-
ActiveSupport::Notifications.subscribe("transmit_subscription_confirmation.action_cable") { |e| events << e }
224+
expected_payload = { channel_class: "ActionCable::Channel::BaseTest::ChatChannel", identifier: "{id: 1}" }
238225

239-
@channel.stub(:subscription_confirmation_sent?, false) do
240-
@channel.send(:transmit_subscription_confirmation)
226+
@channel.subscribe_to_channel
241227

242-
assert_equal 1, events.length
243-
assert_equal "transmit_subscription_confirmation.action_cable", events[0].name
244-
assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
245-
assert_equal "{id: 1}", events[0].payload[:identifier]
228+
assert_notifications_count("transmit_subscription_confirmation.action_cable", 1) do
229+
assert_notification("transmit_subscription_confirmation.action_cable", expected_payload) do
230+
@channel.stub(:subscription_confirmation_sent?, false) do
231+
@channel.send(:transmit_subscription_confirmation)
232+
end
233+
end
246234
end
247-
ensure
248-
ActiveSupport::Notifications.unsubscribe "transmit_subscription_confirmation.action_cable"
249235
end
250236

251237
test "notification for transmit_subscription_rejection" do
252-
events = []
253-
ActiveSupport::Notifications.subscribe("transmit_subscription_rejection.action_cable") { |event| events << event }
238+
expected_payload = { channel_class: "ActionCable::Channel::BaseTest::ChatChannel", identifier: "{id: 1}" }
254239

255-
@channel.send(:transmit_subscription_rejection)
256-
257-
assert_equal 1, events.length
258-
assert_equal "transmit_subscription_rejection.action_cable", events[0].name
259-
assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class]
260-
assert_equal "{id: 1}", events[0].payload[:identifier]
261-
ensure
262-
ActiveSupport::Notifications.unsubscribe "transmit_subscription_rejection.action_cable"
240+
assert_notifications_count("transmit_subscription_rejection.action_cable", 1) do
241+
assert_notification("transmit_subscription_rejection.action_cable", expected_payload) do
242+
@channel.send(:transmit_subscription_rejection)
243+
end
244+
end
263245
end
264246

265247
test "behaves like rescuable" do

actioncable/test/server/broadcasting_test.rb

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,37 @@
44
require "stubs/test_server"
55

66
class BroadcastingTest < ActionCable::TestCase
7-
test "fetching a broadcaster converts the broadcasting queue to a string" do
8-
broadcasting = :test_queue
9-
server = TestServer.new
10-
broadcaster = server.broadcaster_for(broadcasting)
7+
setup do
8+
@server = TestServer.new
9+
@broadcasting = "test_queue"
10+
@broadcaster = server.broadcaster_for(@broadcasting)
11+
end
12+
13+
attr_reader :server, :broadcasting, :broadcaster
1114

15+
test "fetching a broadcaster converts the broadcasting queue to a string" do
1216
assert_equal "test_queue", broadcaster.broadcasting
1317
end
1418

1519
test "broadcast generates notification" do
16-
server = TestServer.new
17-
18-
events = []
19-
ActiveSupport::Notifications.subscribe("broadcast.action_cable") { |event| events << event }
20-
21-
broadcasting = "test_queue"
2220
message = { body: "test message" }
23-
server.broadcast(broadcasting, message)
24-
25-
assert_equal 1, events.length
26-
assert_equal "broadcast.action_cable", events[0].name
27-
assert_equal broadcasting, events[0].payload[:broadcasting]
28-
assert_equal message, events[0].payload[:message]
29-
assert_equal ActiveSupport::JSON, events[0].payload[:coder]
30-
ensure
31-
ActiveSupport::Notifications.unsubscribe "broadcast.action_cable"
21+
expected_payload = { broadcasting:, message:, coder: ActiveSupport::JSON }
22+
23+
assert_notifications_count("broadcast.action_cable", 1) do
24+
assert_notification("broadcast.action_cable", expected_payload) do
25+
server.broadcast(broadcasting, message)
26+
end
27+
end
3228
end
3329

3430
test "broadcaster from broadcaster_for generates notification" do
35-
server = TestServer.new
36-
37-
events = []
38-
ActiveSupport::Notifications.subscribe("broadcast.action_cable") { |event| events << event }
39-
40-
broadcasting = "test_queue"
4131
message = { body: "test message" }
32+
expected_payload = { broadcasting:, message:, coder: ActiveSupport::JSON }
4233

43-
broadcaster = server.broadcaster_for(broadcasting)
44-
broadcaster.broadcast(message)
45-
46-
assert_equal 1, events.length
47-
assert_equal "broadcast.action_cable", events[0].name
48-
assert_equal broadcasting, events[0].payload[:broadcasting]
49-
assert_equal message, events[0].payload[:message]
50-
assert_equal ActiveSupport::JSON, events[0].payload[:coder]
51-
ensure
52-
ActiveSupport::Notifications.unsubscribe "broadcast.action_cable"
34+
assert_notifications_count("broadcast.action_cable", 1) do
35+
assert_notification("broadcast.action_cable", expected_payload) do
36+
broadcaster.broadcast(message)
37+
end
38+
end
5339
end
5440
end

0 commit comments

Comments
 (0)