Class: Crspec::Mock::StubChain
- Inherits:
-
Object
- Object
- Crspec::Mock::StubChain
- Defined in:
- lib/crspec/mock/double.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #and_call_original ⇒ Object
- #and_raise(exception = StandardError, message = nil) ⇒ Object
- #and_return(*values) ⇒ Object
- #and_yield(*yield_args) ⇒ Object
-
#initialize(target, method_name) ⇒ StubChain
constructor
A new instance of StubChain.
- #with(*args, **kwargs) ⇒ Object
Constructor Details
#initialize(target, method_name) ⇒ StubChain
Returns a new instance of StubChain.
63 64 65 66 67 68 69 70 71 |
# File 'lib/crspec/mock/double.rb', line 63 def initialize(target, method_name) @target = target @method_name = method_name.to_sym target.__crspec_verify_stub!(@method_name) if target.respond_to?(:__crspec_verify_stub!) @expected_args = nil @expected_kwargs = nil @return_proc = proc {} register_default_stub end |
Instance Attribute Details
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
61 62 63 |
# File 'lib/crspec/mock/double.rb', line 61 def method_name @method_name end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
61 62 63 |
# File 'lib/crspec/mock/double.rb', line 61 def target @target end |
Instance Method Details
#and_call_original ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/crspec/mock/double.rb', line 115 def and_call_original target = @target method_name = @method_name original = begin target.method(method_name) rescue NameError raise MockError, "#{target.inspect} has no original implementation of #{method_name}" end # Find the implementation beneath the interceptor. original = original.super_method while original && original.owner == Interceptor raise MockError, "#{target.inspect} has no original implementation of #{method_name}" unless original @return_proc = proc { |*args, **kwargs, &block| original.call(*args, **kwargs, &block) } update_stub self end |
#and_raise(exception = StandardError, message = nil) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/crspec/mock/double.rb', line 96 def and_raise(exception = StandardError, = nil) @return_proc = proc do err = exception.is_a?(Class) ? exception.new() : exception raise err end update_stub self end |
#and_return(*values) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/crspec/mock/double.rb', line 80 def and_return(*values) if values.size == 1 val = values.first @return_proc = proc { val } else idx = 0 @return_proc = proc do res = values[idx] || values.last idx += 1 res end end update_stub self end |
#and_yield(*yield_args) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/crspec/mock/double.rb', line 105 def and_yield(*yield_args) old_proc = @return_proc @return_proc = proc do |*args, &block| block&.call(*yield_args) old_proc.call(*args) end update_stub self end |
#with(*args, **kwargs) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/crspec/mock/double.rb', line 73 def with(*args, **kwargs) @expected_args = args @expected_kwargs = kwargs update_stub self end |