Class | Spec::Mocks::BaseExpectation |
In: |
lib/spec/mocks/message_expectation.rb
|
Parent: | Object |
sym | [R] |
# File lib/spec/mocks/message_expectation.rb, line 7 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 11: @sym = sym 12: @method_block = method_block 13: @return_block = nil 14: @actual_received_count = 0 15: @expected_received_count = expected_received_count 16: @args_expectation = ArgumentExpectation.new([AnyArgsConstraint.new]) 17: @consecutive = false 18: @exception_to_raise = nil 19: @symbol_to_throw = nil 20: @order_group = expectation_ordering 21: @at_least = nil 22: @at_most = nil 23: @args_to_yield = [] 24: 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 55 55: def and_raise(exception=Exception) 56: @exception_to_raise = exception 57: end
# File lib/spec/mocks/message_expectation.rb, line 30 30: def and_return(*values, &return_block) 31: Kernel::raise AmbiguousReturnError unless @method_block.nil? 32: case values.size 33: when 0 then value = nil 34: when 1 then value = values[0] 35: else 36: value = values 37: @consecutive = true 38: @expected_received_count = values.size if !ignoring_args? && 39: @expected_received_count < values.size 40: end 41: @return_block = block_given? ? return_block : lambda { value } 42: end
# File lib/spec/mocks/message_expectation.rb, line 59 59: def and_throw(symbol) 60: @symbol_to_throw = symbol 61: end
# File lib/spec/mocks/message_expectation.rb, line 63 63: def and_yield(*args) 64: @args_to_yield << args 65: self 66: end
# File lib/spec/mocks/message_expectation.rb, line 26 26: def expected_args 27: @args_expectation.args 28: end
# File lib/spec/mocks/message_expectation.rb, line 72 72: def invoke(args, block) 73: @order_group.handle_order_constraint self 74: 75: begin 76: Kernel::raise @exception_to_raise unless @exception_to_raise.nil? 77: Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil? 78: 79: if !@method_block.nil? 80: default_return_val = invoke_method_block(args) 81: elsif @args_to_yield.size > 0 82: default_return_val = invoke_with_yield(block) 83: else 84: default_return_val = nil 85: end 86: 87: if @consecutive 88: return invoke_consecutive_return_block(args, block) 89: elsif @return_block 90: return invoke_return_block(args, block) 91: else 92: return default_return_val 93: end 94: ensure 95: @actual_received_count += 1 96: end 97: end
# File lib/spec/mocks/message_expectation.rb, line 68 68: def matches(sym, args) 69: @sym == sym and @args_expectation.check_args(args) 70: end
# File lib/spec/mocks/message_expectation.rb, line 121 121: def invoke_consecutive_return_block(args, block) 122: args << block unless block.nil? 123: value = @return_block.call(*args) 124: 125: index = [@actual_received_count, value.size-1].min 126: value[index] 127: end
# File lib/spec/mocks/message_expectation.rb, line 101 101: def invoke_method_block(args) 102: begin 103: @method_block.call(*args) 104: rescue => detail 105: @error_generator.raise_block_failed_error @sym, detail.message 106: end 107: end
# File lib/spec/mocks/message_expectation.rb, line 129 129: def invoke_return_block(args, block) 130: args << block unless block.nil? 131: value = @return_block.call(*args) 132: 133: value 134: end
# File lib/spec/mocks/message_expectation.rb, line 109 109: def invoke_with_yield(block) 110: if block.nil? 111: @error_generator.raise_missing_block_error @args_to_yield 112: end 113: @args_to_yield.each do |args_to_yield_this_time| 114: if block.arity > -1 && args_to_yield_this_time.length != block.arity 115: @error_generator.raise_wrong_arity_error args_to_yield_this_time, block.arity 116: end 117: block.call(*args_to_yield_this_time) 118: end 119: end