@@ -11,58 +11,64 @@ module RSpec
1111 )
1212 end
1313
14- def matcher ( *events , & block )
15- Publish . new ( events , & block )
14+ def matcher ( *expected )
15+ Publish . new ( * expected )
1616 end
1717
1818 specify do
1919 expect {
2020 expect {
2121 true
22- } . to Publish . new ( )
22+ } . to matcher
2323 } . to raise_error ( SyntaxError , "You have to set the event store instance with `in`, e.g. `expect { ... }.to publish(an_event(MyEvent)).in(event_store)`" )
2424 end
2525
2626 specify do
2727 expect {
2828 true
29- } . not_to Publish . new . in ( event_store )
29+ } . not_to matcher . in ( event_store )
3030 end
3131
3232 specify do
3333 expect {
3434 event_store . publish_event ( FooEvent . new )
35- } . to Publish . new . in ( event_store )
35+ } . to matcher . in ( event_store )
3636 end
3737
3838 specify do
3939 expect {
40- true
41- } . to Publish . new { |events |
42- expect ( events ) . to eq [ ]
43- } . in ( event_store )
40+ event_store . publish_event ( FooEvent . new , stream_name : 'Foo$1' )
41+ } . to matcher . in ( event_store ) . in_stream ( 'Foo$1' )
4442 end
4543
4644 specify do
47- foo_event = FooEvent . new
48- bar_event = BarEvent . new
4945 expect {
50- event_store . publish_event ( foo_event )
51- event_store . publish_event ( bar_event )
52- } . to Publish . new . in ( event_store ) { |events |
53- expect ( events ) . to eq [ foo_event , bar_event ]
54- }
46+ event_store . publish_event ( FooEvent . new , stream_name : 'Foo$1' )
47+ } . not_to matcher . in ( event_store ) . in_stream ( 'Bar$1' )
5548 end
5649
5750 specify do
58- foo_event = FooEvent . new
59- bar_event = BarEvent . new
60- event_store . publish_event ( foo_event )
6151 expect {
62- event_store . publish_event ( bar_event )
63- } . to Publish . new . in ( event_store ) { |events |
64- expect ( events ) . to eq [ bar_event ]
65- }
52+ event_store . publish_event ( FooEvent . new )
53+ } . not_to matcher ( matchers . an_event ( BarEvent ) ) . in ( event_store )
54+ end
55+
56+ specify do
57+ expect {
58+ event_store . publish_event ( FooEvent . new )
59+ } . to matcher ( matchers . an_event ( FooEvent ) ) . in ( event_store )
60+ end
61+
62+ specify do
63+ expect {
64+ event_store . publish_event ( FooEvent . new , stream_name : "Foo$1" )
65+ } . to matcher ( matchers . an_event ( FooEvent ) ) . in ( event_store ) . in_stream ( "Foo$1" )
66+ end
67+
68+ specify do
69+ expect {
70+ event_store . publish_event ( FooEvent . new )
71+ } . not_to matcher ( matchers . an_event ( FooEvent ) ) . in ( event_store ) . in_stream ( "Foo$1" )
6672 end
6773
6874 specify do
@@ -71,39 +77,108 @@ def matcher(*events, &block)
7177 expect {
7278 event_store . publish_event ( foo_event , stream_name : "Foo$1" )
7379 event_store . publish_event ( bar_event , stream_name : "Bar$1" )
74- } . to Publish . new . in ( event_store ) . in_stream ( "Foo$1" ) { |events |
75- expect ( events ) . to eq [ foo_event ]
76- }
80+ } . to matcher ( matchers . an_event ( FooEvent ) , matchers . an_event ( BarEvent ) ) . in ( event_store )
7781 end
7882
7983 specify do
84+ foo_event = FooEvent . new
85+ bar_event = BarEvent . new
86+
87+ event_store . publish_event ( foo_event )
8088 expect {
81- event_store . publish_event ( FooEvent . new , stream_name : 'Foo$1' )
82- } . to Publish . new . in ( event_store ) . in_stream ( 'Foo$1' )
89+ event_store . publish_event ( bar_event )
90+ } . not_to matcher ( matchers . an_event ( FooEvent ) ) . in ( event_store )
8391 end
8492
8593 specify do
8694 expect {
87- event_store . publish_event ( FooEvent . new , stream_name : 'Foo$1' )
88- } . not_to Publish . new . in ( event_store ) . in_stream ( 'Bar$1' )
95+ true
96+ } . not_to matcher . in ( event_store )
8997 end
9098
9199 specify do
92- expect {
93- event_store . publish_event ( FooEvent . new )
94- } . to Publish . new ( matchers . an_event ( FooEvent ) ) . in ( event_store )
100+ _matcher = matcher . in ( event_store )
101+ _matcher . matches? ( Proc . new { } )
102+
103+ expect ( _matcher . failure_message_when_negated . to_s ) . to eq ( <<~EOS . strip )
104+ expected block not to have published any events
105+ EOS
95106 end
96107
97108 specify do
98- expect {
99- event_store . publish_event ( FooEvent . new , stream_name : "Foo$1" )
100- } . to Publish . new ( matchers . an_event ( FooEvent ) ) . in ( event_store ) . in_stream ( "Foo$1" )
109+ _matcher = matcher . in ( event_store )
110+ _matcher . matches? ( Proc . new { } )
111+
112+ expect ( _matcher . failure_message . to_s ) . to eq ( <<~EOS . strip )
113+ expected block to have published any events
114+ EOS
101115 end
102116
103117 specify do
104- expect {
105- event_store . publish_event ( FooEvent . new )
106- } . not_to Publish . new ( matchers . an_event ( FooEvent ) ) . in ( event_store ) . in_stream ( "Foo$1" )
118+ _matcher = matcher ( actual = matchers . an_event ( FooEvent ) ) . in ( event_store )
119+ _matcher . matches? ( Proc . new { } )
120+
121+ expect ( _matcher . failure_message . to_s ) . to eq ( <<~EOS )
122+ expected block to have published:
123+
124+ #{ [ actual ] . inspect }
125+
126+ but published:
127+
128+ []
129+ EOS
130+ end
131+
132+ specify do
133+ _matcher = matcher ( actual = matchers . an_event ( FooEvent ) ) . in_stream ( 'foo' ) . in ( event_store )
134+ _matcher . matches? ( Proc . new { } )
135+
136+ expect ( _matcher . failure_message . to_s ) . to eq ( <<~EOS )
137+ expected block to have published:
138+
139+ #{ [ actual ] . inspect }
140+
141+ in stream foo but published:
142+
143+ []
144+ EOS
145+ end
146+
147+ specify do
148+ foo_event = FooEvent . new
149+ _matcher = matcher ( actual = matchers . an_event ( FooEvent ) ) . in ( event_store )
150+ _matcher . matches? ( Proc . new { event_store . publish_event ( foo_event ) } )
151+
152+ expect ( _matcher . failure_message_when_negated . to_s ) . to eq ( <<~EOS )
153+ expected block not to have published:
154+
155+ #{ [ actual ] . inspect }
156+
157+ but published:
158+
159+ #{ [ foo_event ] . inspect }
160+ EOS
161+ end
162+
163+ specify do
164+ foo_event = FooEvent . new
165+ _matcher = matcher ( actual = matchers . an_event ( FooEvent ) ) . in_stream ( 'foo' ) . in ( event_store )
166+ _matcher . matches? ( Proc . new { event_store . publish_event ( foo_event , stream_name : 'foo' ) } )
167+
168+ expect ( _matcher . failure_message_when_negated . to_s ) . to eq ( <<~EOS )
169+ expected block not to have published:
170+
171+ #{ [ actual ] . inspect }
172+
173+ in stream foo but published:
174+
175+ #{ [ foo_event ] . inspect }
176+ EOS
177+ end
178+
179+ specify do
180+ _matcher = matcher
181+ expect ( _matcher . description ) . to eq ( "publish events" )
107182 end
108183 end
109184 end
0 commit comments