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 = lambda {} 14: @received_count = 0 15: @expected_received_count = expected_received_count 16: @args_expectation = ArgumentExpectation.new([:any_args]) 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 = nil 24: end
# File lib/spec/mocks/message_expectation.rb, line 44 44: def and_raise(exception=Exception) 45: @exception_to_raise = exception 46: 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: if values.size == 0 33: value = nil 34: elsif values.size == 1 35: value = values[0] 36: else 37: value = values 38: @consecutive = true 39: @expected_received_count = values.size if @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 48 48: def and_throw(symbol) 49: @symbol_to_throw = symbol 50: end
# File lib/spec/mocks/message_expectation.rb, line 52 52: def and_yield(*args) 53: @args_to_yield = args 54: 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 60 60: def invoke(args, block) 61: @order_group.handle_order_constraint self 62: 63: begin 64: if @exception_to_raise.class == Class 65: @exception_instance_to_raise = @exception_to_raise.new 66: else 67: @exception_instance_to_raise = @exception_to_raise 68: end 69: Kernel::raise @exception_to_raise unless @exception_to_raise.nil? 70: Kernel::throw @symbol_to_throw unless @symbol_to_throw.nil? 71: 72: if !@method_block.nil? 73: return invoke_method_block(args) 74: elsif !@args_to_yield.nil? 75: return invoke_with_yield(block) 76: elsif @consecutive 77: return invoke_consecutive_return_block(args, block) 78: else 79: return invoke_return_block(args, block) 80: end 81: ensure 82: @received_count += 1 83: end 84: end
# File lib/spec/mocks/message_expectation.rb, line 104 104: def invoke_consecutive_return_block(args, block) 105: args << block unless block.nil? 106: value = @return_block.call(*args) 107: 108: index = [@received_count, value.size-1].min 109: value[index] 110: end
# File lib/spec/mocks/message_expectation.rb, line 86 86: def invoke_method_block(args) 87: begin 88: @method_block.call(*args) 89: rescue Spec::Expectations::ExpectationNotMetError => detail 90: @error_generator.raise_block_failed_error @sym, detail.message 91: end 92: end
# File lib/spec/mocks/message_expectation.rb, line 112 112: def invoke_return_block(args, block) 113: args << block unless block.nil? 114: value = @return_block.call(*args) 115: 116: value 117: end
# File lib/spec/mocks/message_expectation.rb, line 94 94: def invoke_with_yield(block) 95: if block.nil? 96: @error_generator.raise_missing_block_error @args_to_yield 97: end 98: if block.arity > -1 && @args_to_yield.length != block.arity 99: @error_generator.raise_wrong_arity_error @args_to_yield, block.arity 100: end 101: block.call(*@args_to_yield) 102: end