Commit 4e2c1d5

mo khan <mo@mokhan.ca>
2014-03-22 05:02:47
ignore events that have no subscribers.
1 parent 3b764cb
Changed files (2)
lib
spec
lib/presentation/event_aggregator.rb
@@ -1,5 +1,5 @@
 class EventAggregator
-  def initialize(subscribers = {})
+  def initialize(subscribers = Hash.new([]))
     @subscribers = subscribers
   end
 
spec/presentation/event_aggregator_spec.rb
@@ -1,19 +1,19 @@
 require "spec_helper"
 
 describe EventAggregator do
-  let(:sut) { EventAggregator.new }
+  let(:subject) { EventAggregator.new }
 
   context "when publishing an event" do
-    let(:hello_subscriber) { double("hello_subscriber", hello: true) }
-    let(:other_hello_subscriber) { double("other_hello_subscriber", hello: true) }
-    let(:goodbye_subscriber) { double("goodbye_subscriber", hello: false) }
+    let(:hello_subscriber) { double("hello_subscriber", hello: true, nada: nil) }
+    let(:other_hello_subscriber) { double("other_hello_subscriber", hello: true, nada: nil) }
+    let(:goodbye_subscriber) { double("goodbye_subscriber", hello: false, nada: nil) }
 
     before :each do
-      sut.subscribe(:hello, hello_subscriber)
-      sut.subscribe(:hello, other_hello_subscriber)
-      sut.subscribe(:goodbye, goodbye_subscriber)
+      subject.subscribe(:hello, hello_subscriber)
+      subject.subscribe(:hello, other_hello_subscriber)
+      subject.subscribe(:goodbye, goodbye_subscriber)
 
-      sut.publish(:hello, 'mo', 'kha')
+      subject.publish(:hello, 'mo', 'kha')
     end
 
     it "notifies all subscribers of that event" do
@@ -24,5 +24,12 @@ describe EventAggregator do
     it "does not notify subscribers of other events" do
       goodbye_subscriber.should_not have_received(:hello)
     end
+
+    it "does nothing when there are not subscribers" do
+      subject.publish(:nada)
+      hello_subscriber.should_not have_received(:nada)
+      other_hello_subscriber.should_not have_received(:nada)
+      goodbye_subscriber.should_not have_received(:nada)
+    end
   end
 end