Class: RSpec::Matchers::BuiltIn::Exist
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::Matchers::BuiltIn::Exist
- Defined in:
- lib/rspec/matchers/built_in/exist.rb
Instance Attribute Summary
Attributes inherited from BaseMatcher
#actual, #expected, #rescued_exception
Instance Method Summary collapse
-
#initialize(*expected) ⇒ Exist
constructor
A new instance of Exist.
- #matches?(actual) ⇒ Boolean
Methods inherited from BaseMatcher
#==, #description, #diffable?, #failure_message_for_should, #failure_message_for_should_not, #match_unless_raises
Methods included from Pretty
#_pretty_print, #expected_to_sentence, #name, #name_to_sentence, #split_words, #to_sentence, #underscore
Constructor Details
#initialize(*expected) ⇒ Exist
Returns a new instance of Exist.
5 6 7 |
# File 'lib/rspec/matchers/built_in/exist.rb', line 5 def initialize(*expected) @expected = expected end |
Instance Method Details
#matches?(actual) ⇒ Boolean
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rspec/matchers/built_in/exist.rb', line 9 def matches?(actual) @actual = actual predicates = [:exist?, :exists?].select { |p| @actual.respond_to?(p) } existence_values = predicates.map { |p| @actual.send(p, *@expected) } uniq_truthy_values = existence_values.map { |v| !!v }.uniq case uniq_truthy_values.size when 0; raise NoMethodError.new("#{@actual.inspect} does not respond to either #exist? or #exists?") when 1; existence_values.first else raise "#exist? and #exists? returned different values:\n\n" + " exist?: #{existence_values.first}\n" + "exists?: #{existence_values.last}" end end |