Class: RSpec::SleepingKingStudios::Matchers::Core::DelegateMethodMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::Core::DelegateMethodMatcher
- Includes:
- Mocks::ExampleMethods
- Defined in:
- lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb
Overview
Matcher for testing whether an object delegates a method to the specified other object.
Constant Summary collapse
- DEFAULT_EXPECTED_RETURN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Object.new.freeze
Constants included from Description
Description::DEFAULT_EXPECTED_ITEMS
Instance Attribute Summary collapse
-
#expected ⇒ Object
(also: #method_name)
readonly
method initialize.
Attributes inherited from BaseMatcher
Instance Method Summary collapse
-
#and_return(*return_values) ⇒ DelegateMethodMatcher
Specifies that the actual method, when called, will return the specified value.
- #description ⇒ Object
-
#failure_message ⇒ Object
Message for when the object does not match, but was expected to.
-
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to.
-
#initialize(expected) ⇒ DelegateMethodMatcher
constructor
A new instance of DelegateMethodMatcher.
-
#matches?(actual) ⇒ Boolean
Tests the actual object to see if it matches the defined condition(s).
-
#to(target) ⇒ DelegateMethodMatcher
Specifies the target object.
-
#with_a_block ⇒ DelegateMethodMatcher
(also: #and_a_block)
Specifies that a block argument must be passed in to the target when calling the method on the actual object.
-
#with_arguments(*arguments) ⇒ DelegateMethodMatcher
(also: #and_arguments)
Specifies a list of arguments.
-
#with_keywords(**keywords) ⇒ DelegateMethodMatcher
(also: #and_keywords)
Specifies a hash of keywords and values.
Methods inherited from BaseMatcher
Constructor Details
#initialize(expected) ⇒ DelegateMethodMatcher
Returns a new instance of DelegateMethodMatcher.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 20 def initialize expected @expected = expected @expected_arguments = [] @expected_keywords = {} @expected_block = false @received_block = false @expected_return_values = [] @received_return_values = [] @errors = {} end |
Instance Attribute Details
#expected ⇒ Object (readonly) Also known as: method_name
method initialize
31 32 33 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 31 def expected @expected end |
Instance Method Details
#and_return(*return_values) ⇒ DelegateMethodMatcher
Specifies that the actual method, when called, will return the specified value. If more than one return value is specified, the method will be called one time for each return value, and on the Nth call must return the Nth specified return value.
from calling the method on the actual object.
43 44 45 46 47 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 43 def and_return *return_values @expected_return_values = return_values self end |
#description ⇒ Object
50 51 52 53 54 55 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 50 def description str = 'delegate method' str << " :#{@expected}" if @expected str << " to #{@target.inspect}" if @target str end |
#failure_message ⇒ Object
Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 58 def = "expected #{@actual.inspect} to delegate :#{@expected} to "\ "#{@target.inspect}" if @errors.key?(:actual_does_not_respond_to) << ", but #{@actual.inspect} does not respond to :#{@expected}" return end # if if @errors.key?(:target_does_not_respond_to) << ", but #{@target.inspect} does not respond to :#{@expected}" return end # if if @errors.key?(:raised_exception) exception = @errors[:raised_exception] << format_arguments << format_return_values << ", but ##{@expected} raised #{exception.class.name}: "\ "#{exception.}" return end # if if @errors.key?(:actual_does_not_delegate_to_target) << ", but calling ##{@expected} on the object does not call "\ "##{@expected} on the delegate" return end # if << format_arguments << format_return_values if @errors.key?(:unexpected_arguments) << ", but #{@errors[:unexpected_arguments]}" return end # if if @errors.key?(:block_not_received) << ', but the block was not passed to the delegate' return end # if if @errors.key?(:unexpected_return) values = @errors[:unexpected_return].map &:inspect << ', but returned ' << SleepingKingStudios::Tools::ArrayTools.humanize_list(values) end # if end |
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.
118 119 120 121 122 123 124 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 118 def = "expected #{@actual.inspect} not to delegate :#{@expected} to "\ "#{@target.inspect}" << format_arguments << format_return_values end |
#matches?(actual) ⇒ Boolean
Tests the actual object to see if it matches the defined condition(s). Invoked by RSpec expectations.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 127 def matches? actual # :nocov: if RUBY_VERSION < '3.0' SleepingKingStudios::Tools::CoreTools.deprecate('DelegateMethodMatcher') else SleepingKingStudios::Tools::CoreTools .new(deprecation_strategy: 'raise') .deprecate('DelegateMethodMatcher') end super raise ArgumentError.new('must specify a target') if @target.nil? responds_to_method? && delegates_method? end |
#to(target) ⇒ DelegateMethodMatcher
Specifies the target object. The expected methods should be delegated from the actual object to the target.
150 151 152 153 154 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 150 def to target @target = target self end |
#with_a_block ⇒ DelegateMethodMatcher Also known as: and_a_block
Specifies that a block argument must be passed in to the target when calling the method on the actual object.
160 161 162 163 164 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 160 def with_a_block @expected_block = true self end |
#with_arguments(*arguments) ⇒ DelegateMethodMatcher Also known as: and_arguments
Specifies a list of arguments. The provided arguments are passed in to the method call when calling the method on the actual object, and must be passed on to the target.
175 176 177 178 179 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 175 def with_arguments *arguments @expected_arguments = arguments self end |
#with_keywords(**keywords) ⇒ DelegateMethodMatcher Also known as: and_keywords
Specifies a hash of keywords and values. The provided keywords are passed in to the method call when calling the method on the actual object, and must be passed on to the target.
190 191 192 193 194 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/delegate_method_matcher.rb', line 190 def with_keywords **keywords @expected_keywords = keywords self end |