2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
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
|
|
|
|
|
|
|
|
def private_party(event)
|
2014-05-09 05:16:18 -04:00
|
|
|
events << event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
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
|
|
|
|
end
|