Module: Mocha::ParameterMatchers::Methods
- Included in:
- API
- Defined in:
- lib/mocha/parameter_matchers.rb,
lib/mocha/parameter_matchers/not.rb,
lib/mocha/parameter_matchers/is_a.rb,
lib/mocha/parameter_matchers/all_of.rb,
lib/mocha/parameter_matchers/any_of.rb,
lib/mocha/parameter_matchers/equals.rb,
lib/mocha/parameter_matchers/has_key.rb,
lib/mocha/parameter_matchers/kind_of.rb,
lib/mocha/parameter_matchers/anything.rb,
lib/mocha/parameter_matchers/has_keys.rb,
lib/mocha/parameter_matchers/includes.rb,
lib/mocha/parameter_matchers/has_entry.rb,
lib/mocha/parameter_matchers/has_value.rb,
lib/mocha/parameter_matchers/optionally.rb,
lib/mocha/parameter_matchers/has_entries.rb,
lib/mocha/parameter_matchers/instance_of.rb,
lib/mocha/parameter_matchers/responds_with.rb,
lib/mocha/parameter_matchers/any_parameters.rb,
lib/mocha/parameter_matchers/equivalent_uri.rb,
lib/mocha/parameter_matchers/regexp_matches.rb,
lib/mocha/parameter_matchers/yaml_equivalent.rb
Overview
These methods build instances of the Mocha::ParameterMatchers classes which are used with Expectation#with to restrict the parameter values. Can be nested, e.g. see #all_of examples.
Instance Method Summary collapse
-
#all_of(*matchers) ⇒ AllOf
Matches if all
matchersmatch. -
#any_of(*matchers) ⇒ AnyOf
Matches if any
matchersmatch. -
#any_parameters ⇒ AnyParameters
Matches any parameters.
-
#anything ⇒ Anything
Matches any object.
-
#equals(value) ⇒ Equals
Matches any
Objectequallingvalue. -
#equivalent_uri(uri) ⇒ EquivalentUri
Matches a URI without regard to the ordering of parameters in the query string.
-
#has_entries(entries) ⇒ HasEntries
Matches
Hashcontaining allentries. -
#has_entry(*options) ⇒ HasEntry
Matches
Hashcontaining entry withkeyandvalue. -
#has_key(key) ⇒ HasKey
Matches
Hashcontainingkey. -
#has_keys(*keys) ⇒ HasKeys
Matches
Hashcontainingkeys. -
#has_value(value) ⇒ HasValue
Matches
Hashcontainingvalue. -
#includes(*items) ⇒ Includes
Matches any object that responds with
trueto include?(item) for all items. -
#instance_of(klass) ⇒ InstanceOf
Matches any object that is an instance of
klass. -
#is_a(klass) ⇒ IsA
Matches any object that is a
klass. -
#kind_of(klass) ⇒ KindOf
Matches any
Objectthat is a kind ofklass. -
#Not(matcher) ⇒ Not
Matches if
matcherdoes not match. -
#optionally(*matchers) ⇒ Optionally
Matches optional parameters if available.
-
#regexp_matches(regexp) ⇒ RegexpMatches
Matches any object that matches
regexp. -
#responds_with(*options) ⇒ RespondsWith
Parameter matcher.
-
#yaml_equivalent(object) ⇒ YamlEquivalent
Matches any YAML that represents the specified
object.
Instance Method Details
#all_of(*matchers) ⇒ AllOf
Matches if all matchers match.
26 27 28 |
# File 'lib/mocha/parameter_matchers/all_of.rb', line 26 def all_of(*matchers) AllOf.new(*matchers) end |
#any_of(*matchers) ⇒ AnyOf
Matches if any matchers match.
32 33 34 |
# File 'lib/mocha/parameter_matchers/any_of.rb', line 32 def any_of(*matchers) AnyOf.new(*matchers) end |
#any_parameters ⇒ AnyParameters
Matches any parameters. This is used as the default for a newly built expectation.
24 25 26 |
# File 'lib/mocha/parameter_matchers/any_parameters.rb', line 24 def any_parameters AnyParameters.new end |
#anything ⇒ Anything
Matches any object.
21 22 23 |
# File 'lib/mocha/parameter_matchers/anything.rb', line 21 def anything Anything.new end |
#equals(value) ⇒ Equals
Matches any Object equalling value.
27 28 29 |
# File 'lib/mocha/parameter_matchers/equals.rb', line 27 def equals(value) Equals.new(value) end |
#equivalent_uri(uri) ⇒ EquivalentUri
Matches a URI without regard to the ordering of parameters in the query string.
27 28 29 |
# File 'lib/mocha/parameter_matchers/equivalent_uri.rb', line 27 def equivalent_uri(uri) EquivalentUri.new(uri) end |
#has_entries(entries) ⇒ HasEntries
Matches Hash containing all entries.
29 30 31 |
# File 'lib/mocha/parameter_matchers/has_entries.rb', line 29 def has_entries(entries) # rubocop:disable Naming/PredicatePrefix HasEntries.new(entries) end |
#has_entry(key, value) ⇒ HasEntry #has_entry(single_entry_hash) ⇒ HasEntry
Matches Hash containing entry with key and value.
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mocha/parameter_matchers/has_entry.rb', line 46 def has_entry(*) # rubocop:disable Naming/PredicatePrefix case .length when 0 raise ArgumentError, 'No arguments. Expecting at least one.' when 1 key, value = HasEntry.parse_option([0]) when 2 key, value = else raise ArgumentError, 'Too many arguments; use either a single argument (must be a Hash) or two arguments (a key and a value).' end HasEntry.new(key, value) end |
#has_key(key) ⇒ HasKey
Matches Hash containing key.
27 28 29 |
# File 'lib/mocha/parameter_matchers/has_key.rb', line 27 def has_key(key) # rubocop:disable Naming/PredicatePrefix HasKey.new(key) end |
#has_keys(*keys) ⇒ HasKeys
Matches Hash containing keys.
27 28 29 |
# File 'lib/mocha/parameter_matchers/has_keys.rb', line 27 def has_keys(*keys) # rubocop:disable Naming/PredicatePrefix HasKeys.new(*keys) end |
#has_value(value) ⇒ HasValue
Matches Hash containing value.
27 28 29 |
# File 'lib/mocha/parameter_matchers/has_value.rb', line 27 def has_value(value) # rubocop:disable Naming/PredicatePrefix HasValue.new(value) end |
#includes(*items) ⇒ Includes
Matches any object that responds with true to include?(item) for all items.
66 67 68 |
# File 'lib/mocha/parameter_matchers/includes.rb', line 66 def includes(*items) Includes.new(*items) end |
#instance_of(klass) ⇒ InstanceOf
Matches any object that is an instance of klass
27 28 29 |
# File 'lib/mocha/parameter_matchers/instance_of.rb', line 27 def instance_of(klass) InstanceOf.new(klass) end |
#is_a(klass) ⇒ IsA
Matches any object that is a klass.
28 29 30 |
# File 'lib/mocha/parameter_matchers/is_a.rb', line 28 def is_a(klass) # rubocop:disable Naming/PredicatePrefix IsA.new(klass) end |
#kind_of(klass) ⇒ KindOf
Matches any Object that is a kind of klass.
27 28 29 |
# File 'lib/mocha/parameter_matchers/kind_of.rb', line 27 def kind_of(klass) KindOf.new(klass) end |
#Not(matcher) ⇒ Not
Matches if matcher does not match.
27 28 29 |
# File 'lib/mocha/parameter_matchers/not.rb', line 27 def Not(matcher) # rubocop:disable Naming/MethodName Not.new(matcher) end |
#optionally(*matchers) ⇒ Optionally
Matches optional parameters if available.
36 37 38 |
# File 'lib/mocha/parameter_matchers/optionally.rb', line 36 def optionally(*matchers) Optionally.new(*matchers) end |
#regexp_matches(regexp) ⇒ RegexpMatches
Matches any object that matches regexp.
27 28 29 |
# File 'lib/mocha/parameter_matchers/regexp_matches.rb', line 27 def regexp_matches(regexp) RegexpMatches.new(regexp) end |
#responds_with(message, result) ⇒ RespondsWith #responds_with(messages_vs_results) ⇒ RespondsWith
Returns parameter matcher.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mocha/parameter_matchers/responds_with.rb', line 40 def responds_with(*) case .length when 0 raise ArgumentError, 'No arguments. Expecting at least one.' when 1 option = .first raise ArgumentError, 'Argument is not a Hash.' unless option.is_a?(Hash) raise ArgumentError, 'Argument has no entries.' if option.empty? matchers = option.map { |, result| RespondsWith.new(, result) } AllOf.new(*matchers) when 2 , result = RespondsWith.new(, result) else raise ArgumentError, 'Too many arguments; use either a single argument (must be a Hash) or two arguments (a message and a result).' end end |
#yaml_equivalent(object) ⇒ YamlEquivalent
Matches any YAML that represents the specified object
27 28 29 |
# File 'lib/mocha/parameter_matchers/yaml_equivalent.rb', line 27 def yaml_equivalent(object) YamlEquivalent.new(object) end |