Class: Spec::Mocks::MockHandler
- Defined in:
- lib/spec/mocks/mock_handler.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :null_object => false, :auto_verify => true }
Instance Method Summary collapse
- #add_message_expectation(expected_from, sym, opts = {}, &block) ⇒ Object
- #add_negative_message_expectation(expected_from, sym, &block) ⇒ Object
- #add_stub(expected_from, sym) ⇒ Object
- #has_negative_expectation?(sym) ⇒ Boolean
-
#initialize(target, name, options = {}) ⇒ MockHandler
constructor
A new instance of MockHandler.
- #message_received(sym, *args, &block) ⇒ Object
- #null_object? ⇒ Boolean
- #raise_unexpected_message_args_error(expectation, *args) ⇒ Object
- #raise_unexpected_message_error(sym, *args) ⇒ Object
- #received_message?(sym, *args, &block) ⇒ Boolean
- #reset ⇒ Object
-
#verify ⇒ Object
:nodoc:.
Constructor Details
#initialize(target, name, options = {}) ⇒ MockHandler
Returns a new instance of MockHandler.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/spec/mocks/mock_handler.rb', line 9 def initialize(target, name, ={}) @target = target @name = name @error_generator = ErrorGenerator.new target, name @expectation_ordering = OrderGroup.new @error_generator @expectations = [] @messages_received = [] @stubs = [] @proxied_methods = [] @options = ? DEFAULT_OPTIONS.dup.merge() : DEFAULT_OPTIONS end |
Instance Method Details
#add_message_expectation(expected_from, sym, opts = {}, &block) ⇒ Object
25 26 27 28 29 |
# File 'lib/spec/mocks/mock_handler.rb', line 25 def (expected_from, sym, opts={}, &block) __add expected_from, sym, block @expectations << MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts) @expectations.last end |
#add_negative_message_expectation(expected_from, sym, &block) ⇒ Object
31 32 33 34 35 |
# File 'lib/spec/mocks/mock_handler.rb', line 31 def (expected_from, sym, &block) __add expected_from, sym, block @expectations << NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil) @expectations.last end |
#add_stub(expected_from, sym) ⇒ Object
37 38 39 40 41 |
# File 'lib/spec/mocks/mock_handler.rb', line 37 def add_stub(expected_from, sym) __add expected_from, sym, nil @stubs.unshift MethodStub.new(@error_generator, @expectation_ordering, expected_from, sym, nil) @stubs.first end |
#has_negative_expectation?(sym) ⇒ Boolean
63 64 65 |
# File 'lib/spec/mocks/mock_handler.rb', line 63 def has_negative_expectation?(sym) @expectations.detect {|expectation| expectation.negative_expectation_for?(sym)} end |
#message_received(sym, *args, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/spec/mocks/mock_handler.rb', line 67 def (sym, *args, &block) if expectation = find_matching_expectation(sym, *args) expectation.invoke(args, block) elsif stub = find_matching_method_stub(sym) stub.invoke([], block) elsif expectation = find_almost_matching_expectation(sym, *args) (expectation, *args) unless has_negative_expectation?(sym) unless null_object? else @target.send :method_missing, sym, *args, &block end end |
#null_object? ⇒ Boolean
21 22 23 |
# File 'lib/spec/mocks/mock_handler.rb', line 21 def null_object? @options[:null_object] end |
#raise_unexpected_message_args_error(expectation, *args) ⇒ Object
79 80 81 |
# File 'lib/spec/mocks/mock_handler.rb', line 79 def (expectation, *args) @error_generator. expectation, *args end |
#raise_unexpected_message_error(sym, *args) ⇒ Object
83 84 85 |
# File 'lib/spec/mocks/mock_handler.rb', line 83 def (sym, *args) @error_generator. sym, *args end |
#received_message?(sym, *args, &block) ⇒ Boolean
58 59 60 61 |
# File 'lib/spec/mocks/mock_handler.rb', line 58 def (sym, *args, &block) return true if @messages_received.find {|array| array == [sym, args, block]} return false end |
#reset ⇒ Object
51 52 53 54 55 56 |
# File 'lib/spec/mocks/mock_handler.rb', line 51 def reset clear_expectations clear_stubs reset_proxied_methods clear_proxied_methods end |
#verify ⇒ Object
:nodoc:
43 44 45 46 47 48 49 |
# File 'lib/spec/mocks/mock_handler.rb', line 43 def verify #:nodoc: begin verify_expectations ensure reset end end |