main
 1require "spec_helper"
 2
 3describe EventAggregator do
 4  let(:subject) { EventAggregator.new }
 5
 6  context "when publishing an event" do
 7    let(:hello_subscriber) { double("hello_subscriber", hello: true, nada: nil) }
 8    let(:other_hello_subscriber) { double("other_hello_subscriber", hello: true, nada: nil) }
 9    let(:goodbye_subscriber) { double("goodbye_subscriber", hello: false, nada: nil) }
10
11    before :each do
12      subject.subscribe(:hello, hello_subscriber)
13      subject.subscribe(:hello, other_hello_subscriber)
14      subject.subscribe(:goodbye, goodbye_subscriber)
15
16      subject.publish(:hello, 'mo', 'kha')
17    end
18
19    it "notifies all subscribers of that event" do
20      hello_subscriber.should have_received(:hello).with('mo', 'kha')
21      other_hello_subscriber.should have_received(:hello).with('mo', 'kha')
22    end
23
24    it "does not notify subscribers of other events" do
25      goodbye_subscriber.should_not have_received(:hello)
26    end
27
28    it "does nothing when there are not subscribers" do
29      subject.publish(:nada)
30      hello_subscriber.should_not have_received(:nada)
31      other_hello_subscriber.should_not have_received(:nada)
32      goodbye_subscriber.should_not have_received(:nada)
33    end
34  end
35end