2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
require "active_support/subscriber"
|
2013-09-19 05:03:58 -04:00
|
|
|
|
|
|
|
class TestSubscriber < ActiveSupport::Subscriber
|
|
|
|
attach_to :doodle
|
|
|
|
|
2014-05-09 05:16:18 -04:00
|
|
|
cattr_reader :events
|
2013-09-19 05:03:58 -04:00
|
|
|
|
|
|
|
def self.clear
|
2014-05-09 05:16:18 -04:00
|
|
|
@@events = []
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def open_party(event)
|
2014-05-09 05:16:18 -04:00
|
|
|
events << event
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def private_party(event)
|
|
|
|
events << event
|
|
|
|
end
|
2014-05-09 05:16:18 -04:00
|
|
|
end
|
|
|
|
|
2019-04-04 01:27:57 -04:00
|
|
|
class TestSubscriber2 < ActiveSupport::Subscriber
|
|
|
|
attach_to :doodle
|
|
|
|
detach_from :doodle
|
|
|
|
|
|
|
|
cattr_reader :events
|
|
|
|
|
|
|
|
def self.clear
|
|
|
|
@@events = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_party(event)
|
|
|
|
events << event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-09 05:16:18 -04:00
|
|
|
# Monkey patch subscriber to test that only one subscriber per method is added.
|
|
|
|
class TestSubscriber
|
2014-05-23 10:45:28 -04:00
|
|
|
remove_method :open_party
|
2014-05-09 05:16:18 -04:00
|
|
|
def open_party(event)
|
|
|
|
events << event
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SubscriberTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
TestSubscriber.clear
|
2019-04-04 01:27:57 -04:00
|
|
|
TestSubscriber2.clear
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_attaches_subscribers
|
|
|
|
ActiveSupport::Notifications.instrument("open_party.doodle")
|
|
|
|
|
2014-05-09 05:16:18 -04:00
|
|
|
assert_equal "open_party.doodle", TestSubscriber.events.first.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attaches_only_one_subscriber
|
|
|
|
ActiveSupport::Notifications.instrument("open_party.doodle")
|
|
|
|
|
|
|
|
assert_equal 1, TestSubscriber.events.size
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_does_not_attach_private_methods
|
|
|
|
ActiveSupport::Notifications.instrument("private_party.doodle")
|
|
|
|
|
2014-07-31 07:56:22 -04:00
|
|
|
assert_equal [], TestSubscriber.events
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
2019-04-04 01:27:57 -04:00
|
|
|
|
|
|
|
def test_detaches_subscribers
|
|
|
|
ActiveSupport::Notifications.instrument("open_party.doodle")
|
|
|
|
|
|
|
|
assert_equal [], TestSubscriber2.events
|
|
|
|
assert_equal 1, TestSubscriber.events.size
|
|
|
|
end
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|