77
88module Sus
99 class Receive
10- CALL_ORIGINAL = Object . new
11-
12- def initialize ( base , method )
10+ def initialize ( base , method , &block )
1311 @base = base
1412 @method = method
1513
1614 @times = Times . new
1715 @arguments = nil
1816 @options = nil
1917 @block = nil
20- @returning = CALL_ORIGINAL
18+
19+ @returning = block
2120 end
2221
2322 def print ( output )
@@ -60,12 +59,27 @@ def with_call_count(predicate)
6059 return self
6160 end
6261
63- def and_return ( *returning )
64- if returning . size == 1
65- @returning = returning . first
62+ def and_return ( *returning , &block )
63+ if block_given?
64+ if returning . any?
65+ raise ArgumentError , "Cannot specify both a block and returning values."
66+ end
67+
68+ @returning = block
69+ elsif returning . size == 1
70+ @returning = proc { returning . first }
6671 else
67- @returning = returning
72+ @returning = proc { returning }
73+ end
74+
75+ return self
76+ end
77+
78+ def and_raise ( ...)
79+ @returning = proc do
80+ raise ( ...)
6881 end
82+
6983 return self
7084 end
7185
@@ -97,7 +111,7 @@ def call(assertions, subject)
97111
98112 validate ( mock , assertions , arguments , options , block )
99113
100- next @returning
114+ next @returning . call ( * arguments , ** options , & block )
101115 end
102116 end
103117
@@ -110,7 +124,7 @@ def call(assertions, subject)
110124 end
111125
112126 def call_original?
113- @returning == CALL_ORIGINAL
127+ @returning . nil?
114128 end
115129
116130 class WithArguments
@@ -128,7 +142,7 @@ def call(assertions, subject)
128142 end
129143 end
130144 end
131-
145+
132146 class WithOptions
133147 def initialize ( predicate )
134148 @predicate = predicate
@@ -153,15 +167,15 @@ def initialize(predicate)
153167 def print ( output )
154168 output . write ( "with block" , @predicate )
155169 end
156-
170+
157171 def call ( assertions , subject )
158172 assertions . nested ( self ) do |assertions |
159173
160174 Expect . new ( assertions , subject ) . not . to ( Be == nil )
161175 end
162176 end
163177 end
164-
178+
165179 class Times
166180 ONCE = Be . new ( :== , 1 )
167181
@@ -172,7 +186,7 @@ def initialize(condition = ONCE)
172186 def print ( output )
173187 output . write ( "with call count " , @condition )
174188 end
175-
189+
176190 def call ( assertions , subject )
177191 assertions . nested ( self ) do |assertions |
178192 Expect . new ( assertions , subject ) . to ( @condition )
@@ -182,8 +196,8 @@ def call(assertions, subject)
182196 end
183197
184198 class Base
185- def receive ( method )
186- Receive . new ( self , method )
199+ def receive ( method , & block )
200+ Receive . new ( self , method , & block )
187201 end
188202 end
189203end
0 commit comments