-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbus_spec.rb
72 lines (48 loc) · 1.84 KB
/
bus_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'spec_helper'
require 'bus'
describe Bus do
let (:listener) { double }
it 'calls attached listener' do
bus = subject.attach(listener)
expect(listener).to receive(:event_fired)
bus.event_fired
end
it 'calls attached listener with params' do
bus = subject.attach(listener)
expect(listener).to receive(:event_fired).with(1, 2, 3)
bus.event_fired(1, 2, 3)
end
it 'does not call listener if it does not respond to event' do
non_responding_listener = double(respond_to?: false)
responding_listener = double.as_null_object
bus = subject.attach(non_responding_listener).attach(responding_listener)
expect(non_responding_listener).not_to receive(:event_fired)
bus.event_fired
end
it 'calls multiple listeners' do
listener1 = double
listener2 = double
bus = subject.attach(listener1).attach(listener2)
expect(listener1).to receive(:event_fired)
expect(listener2).to receive(:event_fired)
bus.event_fired
end
it 'calls lightweight listener defined from hash' do
bus = subject.on(event_fired: listener) # event_fired: ->(args) { do stuff }
expect(listener).to receive(:call).with(:args)
bus.event_fired(:args)
end
it 'calls lightweight listener defined from block' do
bus = subject.on(:event_fired) {|args| listener.call(args)} # event_fired { do stuff }
expect(listener).to receive(:call).with(:args)
bus.event_fired(:args)
end
it 'aliases on and when methods' do
bus = subject.when(:event_fired) {|args| listener.call(args)} # event_fired { do stuff }
expect(listener).to receive(:call).with(:args)
bus.event_fired(:args)
end
it 'throws an exception when no listener responds' do
expect { subject.event_fired }.to raise_error(Bus::NoListenerRespondedError, 'No listener responded to message \'event_fired\'')
end
end