7 def initialize(error_generator, expectation_ordering, expected_from, sym, method_block, expected_received_count=1, opts={})
8 @error_generator = error_generator
9 @error_generator.opts = opts
10 @expected_from = expected_from
12 @method_block = method_block
14 @actual_received_count = 0
15 @expected_received_count = expected_received_count
16 @args_expectation = ArgumentExpectation.new([AnyArgsConstraint.new])
18 @exception_to_raise = nil
19 @symbol_to_throw = nil
20 @order_group = expectation_ordering
27 @args_expectation.args
30 def and_return(*values, &return_block)
31 Kernel::raise AmbiguousReturnError unless @method_block.nil?
33 when 0 then value = nil
34 when 1 then value = values[0]
38 @expected_received_count = values.size if !ignoring_args? &&
39 @expected_received_count < values.size
41 @return_block = block_given? ? return_block : lambda { value }
42 # Ruby 1.9 - see where this is used below
43 @ignore_args = !block_given?
48 # and_raise(Exception) #any exception class
49 # and_raise(exception) #any exception object
53 # When you pass an exception class, the MessageExpectation will
54 # raise an instance of it, creating it with +new+. If the exception
55 # class initializer requires any parameters, you must pass in an
56 # instance and not the class.
57 def and_raise(exception=Exception)
58 @exception_to_raise = exception
62 @symbol_to_throw = symbol
66 @args_to_yield << args
70 def matches(sym, args)
71 @sym == sym and @args_expectation.check_args(args)
74 def invoke(args, block)
75 @order_group.handle_order_constraint self
78 Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
79 Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
81 if !@method_block.nil?
82 default_return_val = invoke_method_block(args)
83 elsif @args_to_yield.size > 0
84 default_return_val = invoke_with_yield(block)
86 default_return_val = nil
90 return invoke_consecutive_return_block(args, block)
92 return invoke_return_block(args, block)
94 return default_return_val
97 @actual_received_count += 1
103 def invoke_method_block(args)
105 @method_block.call(*args)
107 @error_generator.raise_block_failed_error @sym, detail.message
111 def invoke_with_yield(block)
113 @error_generator.raise_missing_block_error @args_to_yield
115 @args_to_yield.each do |args_to_yield_this_time|
116 if block.arity > -1 && args_to_yield_this_time.length != block.arity
117 @error_generator.raise_wrong_arity_error args_to_yield_this_time, block.arity
119 block.call(*args_to_yield_this_time)
123 def invoke_consecutive_return_block(args, block)
124 args << block unless block.nil?
125 value = @return_block.call(*args)
127 index = [@actual_received_count, value.size-1].min
131 def invoke_return_block(args, block)
132 args << block unless block.nil?
133 # Ruby 1.9 - when we set @return_block to return values
134 # regardless of arguments, any arguments will result in
135 # a "wrong number of arguments" error
139 @return_block.call(*args)
144 class MessageExpectation < BaseExpectation
146 def matches_name_but_not_args(sym, args)
147 @sym == sym and not @args_expectation.check_args(args)
150 def verify_messages_received
151 return if ignoring_args? || matches_exact_count? ||
152 matches_at_least_count? || matches_at_most_count?
155 rescue Spec::Mocks::MockExpectationError => error
156 error.backtrace.insert(0, @expected_from)
161 @expected_received_count == :any
164 def matches_at_least_count?
165 @at_least && @actual_received_count >= @expected_received_count
168 def matches_at_most_count?
169 @at_most && @actual_received_count <= @expected_received_count
172 def matches_exact_count?
173 @expected_received_count == @actual_received_count
177 @error_generator.raise_expectation_error(@sym, @expected_received_count, @actual_received_count, *@args_expectation.args)
180 def with(*args, &block)
181 @method_block = block if block
182 @args_expectation = ArgumentExpectation.new(args)
187 set_expected_received_count :exactly, n
192 set_expected_received_count :at_least, n
197 set_expected_received_count :at_most, n
202 @method_block = block if block
206 def any_number_of_times(&block)
207 @method_block = block if block
208 @expected_received_count = :any
213 @expected_received_count = 0
218 @method_block = block if block
219 @expected_received_count = 1
224 @method_block = block if block
225 @expected_received_count = 2
230 @method_block = block if block
231 @order_group.register(self)
236 def negative_expectation_for?(sym)
241 def set_expected_received_count(relativity, n)
242 @at_least = (relativity == :at_least)
243 @at_most = (relativity == :at_most)
244 @expected_received_count = case n
256 class NegativeMessageExpectation < MessageExpectation
257 def initialize(message, expectation_ordering, expected_from, sym, method_block)
258 super(message, expectation_ordering, expected_from, sym, method_block, 0)
261 def negative_expectation_for?(sym)