# File lib/spec/mocks/message_expectation.rb, line 11
11: def initialize(error_generator, expectation_ordering, expected_from, sym, method_block, expected_received_count=1, opts={}, &implementation)
12: @error_generator = error_generator
13: @error_generator.opts = opts
14: @expected_from = expected_from
15: @sym = sym
16: @method_block = method_block
17: @actual_received_count = 0
18: @expected_received_count = expected_received_count
19: @args_expectation = ArgumentExpectation.new([ArgumentMatchers::AnyArgsMatcher.new])
20: @consecutive = false
21: @exception_to_raise = nil
22: @symbol_to_throw = nil
23: @order_group = expectation_ordering
24: @at_least = nil
25: @at_most = nil
26: @args_to_yield = []
27: @failed_fast = nil
28: @args_to_yield_were_cloned = false
29: @return_block = implementation
30: @eval_context = nil
31: end
When you pass an exception class, the MessageExpectation will raise an instance of it, creating it with new. If the exception class initializer requires any parameters, you must pass in an instance and not the class.
# File lib/spec/mocks/message_expectation.rb, line 75
75: def and_raise(exception=Exception)
76: @exception_to_raise = exception
77: end
# File lib/spec/mocks/message_expectation.rb, line 50
50: def and_return(*values, &return_block)
51: Kernel::raise AmbiguousReturnError unless @method_block.nil?
52: case values.size
53: when 0 then value = nil
54: when 1 then value = values[0]
55: else
56: value = values
57: @consecutive = true
58: @expected_received_count = values.size if !ignoring_args? &&
59: @expected_received_count < values.size
60: end
61: @return_block = block_given? ? return_block : lambda { value }
62: end
# File lib/spec/mocks/message_expectation.rb, line 79
79: def and_throw(symbol)
80: @symbol_to_throw = symbol
81: end
# File lib/spec/mocks/message_expectation.rb, line 83
83: def and_yield(*args, &block)
84: if @args_to_yield_were_cloned
85: @args_to_yield.clear
86: @args_to_yield_were_cloned = false
87: end
88:
89: if block
90: require 'spec/extensions/instance_exec'
91: @eval_context = Object.new
92: @eval_context.extend Spec::Matchers::InstanceExec
93: yield @eval_context
94: end
95: @args_to_yield << args
96: self
97: end
# File lib/spec/mocks/message_expectation.rb, line 33
33: def build_child(expected_from, method_block, expected_received_count, opts={})
34: child = clone
35: child.expected_from = expected_from
36: child.method_block = method_block
37: child.expected_received_count = expected_received_count
38: child.clear_actual_received_count!
39: new_gen = error_generator.clone
40: new_gen.opts = opts
41: child.error_generator = new_gen
42: child.clone_args_to_yield @args_to_yield
43: child
44: end
# File lib/spec/mocks/message_expectation.rb, line 137
137: def called_max_times?
138: @expected_received_count != :any && @expected_received_count > 0 &&
139: @actual_received_count >= @expected_received_count
140: end
# File lib/spec/mocks/message_expectation.rb, line 46
46: def expected_args
47: @args_expectation.args
48: end
# File lib/spec/mocks/message_expectation.rb, line 103
103: def invoke(*args, &block)
104: if @expected_received_count == 0
105: @failed_fast = true
106: @actual_received_count += 1
107: @error_generator.raise_expectation_error @sym, @expected_received_count, @actual_received_count, *args
108: end
109:
110: @order_group.handle_order_constraint self
111:
112: begin
113: Kernel::raise @exception_to_raise unless @exception_to_raise.nil?
114: Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil?
115:
116:
117: if !@method_block.nil?
118: default_return_val = invoke_method_block(*args)
119: elsif @args_to_yield.size > 0 || @eval_context
120: default_return_val = invoke_with_yield(&block)
121: else
122: default_return_val = nil
123: end
124:
125: if @consecutive
126: return invoke_consecutive_return_block(*args, &block)
127: elsif @return_block
128: return invoke_return_block(*args, &block)
129: else
130: return default_return_val
131: end
132: ensure
133: @actual_received_count += 1
134: end
135: end
# File lib/spec/mocks/message_expectation.rb, line 142
142: def invoke_return_block(*args, &block)
143: args << block unless block.nil?
144: # Ruby 1.9 - when we set @return_block to return values
145: # regardless of arguments, any arguments will result in
146: # a "wrong number of arguments" error
147: @return_block.arity == 0 ? @return_block.call : @return_block.call(*args)
148: end
# File lib/spec/mocks/message_expectation.rb, line 188
188: def clone_args_to_yield(args)
189: @args_to_yield = args.clone
190: @args_to_yield_were_cloned = true
191: end
# File lib/spec/mocks/message_expectation.rb, line 174
174: def eval_block(*args, &block)
175: if @eval_context
176: @eval_context.instance_exec(*args, &block)
177: else
178: block.call(*args)
179: end
180: end
# File lib/spec/mocks/message_expectation.rb, line 193
193: def failed_fast?
194: @failed_fast
195: end
# File lib/spec/mocks/message_expectation.rb, line 182
182: def invoke_consecutive_return_block(*args, &block)
183: value = invoke_return_block(*args, &block)
184: index = [@actual_received_count, value.size-1].min
185: value[index]
186: end
# File lib/spec/mocks/message_expectation.rb, line 152
152: def invoke_method_block(*args)
153: begin
154: @method_block.call(*args)
155: rescue => detail
156: @error_generator.raise_block_failed_error @sym, detail.message
157: end
158: end
# File lib/spec/mocks/message_expectation.rb, line 160
160: def invoke_with_yield(&block)
161: if block.nil?
162: @error_generator.raise_missing_block_error @args_to_yield
163: end
164: value = nil
165: @args_to_yield.each do |args_to_yield_this_time|
166: if block.arity > 1 && args_to_yield_this_time.length != block.arity
167: @error_generator.raise_wrong_arity_error args_to_yield_this_time, block.arity
168: end
169: value = eval_block(*args_to_yield_this_time, &block)
170: end
171: value
172: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.